c# - Identical entities generating different ids -


how can ensure ef generates same id same occurence of object?

e.g. if have class like:

class foo{     icollection<bar> bar1;     icollection<bar> bar2; } 

if create new entity foo , add single instance of bar (no id set) both bar1 , bar2 - use same id both or create new 1 each time? e.g.:

var bar = new bar(); var foo = new foo{    bar1 = new list<bar>(){ bar };    bar2 = new list<bar>(){ bar }; } context.savechanges(); 

will bar1 , bar2 contain same record bar same id?

update: if have 2 instances same respect equals , hash code?

var first = new bar(); var second = new bar();   /// first.equals(second ) var foo = new foo{    bar1 = new list<bar>(){ first };    bar2 = new list<bar>(){ second }; } 

or ef not check equals?

yes, entity framework create 1 record foo , 1 record bar in database. since both lists contain same instance of bar respective database entries point same record.


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -