Inserting Transaction 'Many to Many" Records With LINQ

While working on a rules engine, 'Rules Editor', I started thinking that there might be a better way in LINQ for handling insertion of records through transactions. Before, I would add all the code necessary, SPs, TransactionScope and ADO.Net plumbing code to handle inserting records within tables that had a 'many to many' relationship. In this example I have three tables.

A RulesApp can have many RulesDefinitions, so to do that, I chose to save the RulesApp, RulesDefinition and their relationship in RuleDefinitionsForRulesApp all at the same time rather than individually. By viewing the code below, you can see how coding the relationships can be focused on instead of worrying about the plumbing, which is definitely how it should be. Let's walk through this to see how it works.

Notice: objRuleDefinitionsForRulesApp is my own custom object that has another custom object,RulesApp that is used to 'Assemble' the LINQ objects.

db.SubmitChanges(); is used to commit adding a RulesApp, RulesDefinition(s) and RuleDefinitionsForRulesApp records for their relationship. As you can see, no plumbing was needed and I could focus on building the relationships of LINQ objects.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: BayerWhite
Posted on: 10/7/2008 at 5:07 AM
Actions: E-mail | Kick it! | DZone it! |
Post Information: Permalink | Comments (70) | Post RSSRSS comment feed


Comments are closed