c# - Entity framework 6 transaction -
i need insert new rows in 2 of tables first table's auto generated id field 1 of field of second table .currently i'm using transaction inserting data. current code given below
using (var context = new applicationdatabaseentities()) { using (var transaction = context.database.begintransaction()) { try { foreach (var item in itemlist) { context.myfirstentity.add(item); context.savechanges(); mysecondentity.myfirstentityid = item.id; context.mysecondentity.add(mysecondentity.myfirstentityid ); context.savechanges(); } transaction.commit(); } catch (exception ex) { transaction.rollback(); console.writeline(ex); } } }
the above code working fine .my question is, correct way? mean if have 1000 or 2000 items insertion current code affect performance
code can improved implicit transaction:
foreach (var item in itemlist) { context.myfirstentity.add(item); mysecondentity.myfirstentity = item; context.mysecondentity.add(mysecondentity); } context.savechanges();
note: instead of id i've used navigation property.
Comments
Post a Comment