Wednesday, 15 May 2013

ASP.NET help inserting data into normalized SQL tables best practice -


Hello, One of the most common situations for putting my client order in my SQL database is: I order the order header and order details The tables have been prepared and I am trying to insert a header record and there are several description lines related to the header record (PK and FK compulsions are the header id).

Currently, I insert my header record, then ask the DB for the last created header and for example, use that ID to insert your wider lines by looping it .

I know that there is a stupid way to put this record on the normalized table and an additional SQL call, which seems unnecessary. Therefore, anyone would know better solution to this problem.

I've found that the problem solving problem is using the unit framework, searching for the last entry header Without doing any extra effort to do it and then insert it in the detailed table. Therefore, if relationships are well defined in the framework of the organization, the framework takes care of this process. For example:

 using  (var rep = new Repo ()) {header hd = new header (); Hd.name = "some names"; Expansion DT = new extension (); Dt.itemname = "Some Item Names"; Hd.Details.Add (DT); Rep.Headers.Add (HD); Rep.savechanges (); }  

Unless all the works are rep.savechanges (); Master / Detailed data is done before it will be inserted properly.

If EF receives a confidentiality error or any other errors, then no data will be inserted.

So this is a good way to insert master / expansion data into SQL database.


No comments:

Post a Comment