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
Post a Comment