c# - EF6 not updating, only inserts -
i have following structure in sql server db
company -> branches -> branchtelephone -> telephonenumber
so, company has many branches has many telephonenumbers. branch telephonenumber many-to-many
here entity classes:
public class company { public company() { this.branches = new hashset<branch>(); } [key] //identity column in sql server public int id{ get; set; } public string name{ get; set; } public virtual icollection<branch> branches{ get; set; } } public partial class branch { /// <summary> /// initializes new instance of <see cref="branch"/> class. /// </summary> public branch() { this.telephonenumbers = new hashset<telephonenumber>(); } [column("branchid")] [key] public int id { get; set; } public int companyid{ get; set; } public virtual company company { get; set; } public virtual icollection<telephonenumber> telephonenumbers { get; set; } } public partial class telephonenumber { public telephonenumber() { this.branches = new hashset<branch>(); } [key] [column("telephoneid")] public int id { get; set; } public virtual icollection<branch> branches { get; set; } }
i try following test setup:
var t1 = new telephonenumber(); var t2 = new telephonenumber(); using(var context = new mydbcontext() ) { var company = new company { name = "c1", branches = { new branch { telephonenumbers = {t1, t2} } } }; context.savechanges(); }
at point watching sqal server profiler , runnign sql, data is
inserted correctly
using(var c2 = new mydbcontext()) { var company2 = c2 .companies.first(x=>x.name == "c1" ); var b1 = company2 .branches.first(); //data matches inserted above b1.telephonenumbers.clear(); b1.telephonenumbers.add(new telephonenumber() ); company2.name = "updated"; c2.savechanges(); }
atfer above save changes followign statements genrated @ sql server
1. update company set name = "updated" -- correct 2. delete branchtelephone 2 entries added during first insert 3. insert new company 4. insert new branch 5. insert new telephone number 6. insert new branch telephone 7. insert new telephone number 8. insert new branch telephone 9. insert new telephone number 10. insert new branch telephone
so updates , deletes expected.
but go recreating step one, have total of 3 telephone numbers instead of 1.
i have no idea going on here. appreciated.
thank you.
i played around , turns out
everything fine if ommit following line: b1.telephonenumbers.add(new telephonenumber() );
by fine mean updates data , remove associated telephone numbers fine. but, if above line in same context forgets else , inserts everythign new i.e. new company, branch , 3 telephone numbers (including 2 deleted ones)
this unacceptable. doing wrong here
the problem turned out 1 entity belonging seperate instance of dbcontext. context1 used entitya instance, added context2.entitybcollection, causing ef insert everything, instead of instance of entitya.
i think sort of bug in ef, because if entitya not attached contextb either should have thrown exception or created entitya in database. instead, contextb sent inserts entire hierarchy of objects attached contextb database.
Comments
Post a Comment