NoviceToDotNet
NoviceToDotNet

Reputation: 10805

How to perform crud operation through linq on database in multiple transaction?

I never used it before, now i have need to perform crud operation through inq to database

I have 3 collection that i want to push in 3 different tables in the database.

All three collections belong to a interface to show something there

I want to know how the update and add new operation should be performed. like in simele sqlcommand like statement i was using single save method that had the update stored procedure and check it whether to add a new new item or it is in edit so if a new entry is made the first table returns it primary that i assign to other collection one of proerty and push it the same way.

My concern is how to do the same here that if a new entry is pushed to database the primary key should b returned that i can map to other collection's property?

Upvotes: 0

Views: 161

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062865

From the comments, you are using LINQ-to-SQL; in which case, the ORM deals with this for you. For example:

var cust = new Customer();
ctx.Customers.InsertOnSubmit(cust);
cust.Orders.Add(new Order { Quantity = 1, Part = "abc" } );
cust.Orders.Add(new Order { Quantity = 1, Part = "def" });
ctx.SubmitChanges();

when you call SubmitChanges, the ORM will handle:

  • performing the necessary inserts and updates
  • updating any necessary metadata (identity, rowversion, etc) on any objects
  • etc

so - after the call to SubmitChanges, you should see that cust has picked up whatever identity it is meant to have. In particular, note that when adding Order etc, I added objects, not identities. This is necessary because we don't know the identity yet. Note also that this uses a single transaction.

Upvotes: 1

Related Questions