c# - Get Identity of all objects after SaveChanges -


i inserting objects @ same time using entity framework below.

context = new mycontext(); foreach (var x in lstx) {      var abc = new abc{ name= x.abcname };     context.abcs.add(abc);      var xyz = new xyz{ name = x.xyzname };     context.xyzs.add(xyz); }  context.savechanges(); 

is possible identity of these inserted objects?

when call savechanges, identity field populated on original entity. obtain id, store reference identity , access after savechanges:

context = new mycontext(); list<abc> addedabcs = new list<abc>(); list<xyz> addedxyzs = new list<xyz>(); foreach (var x in lstx) {      var abc = new abc{ name= x.abcname };     addedabcs.add(abc);     context.abcs.add(abc);      var xyz = new xyz{ name = x.xyzname };     addedxyzs.add(xyz);     context.xyzs.add(xyz); }  context.savechanges();  foreach (var abc in addedabcs)  {     console.writeline("added item id {0}", abc.id); } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -