c# - Two many-to-one table relationships and insertion with Entity Framework -


i have model similar this:

enter image description here

context: idea it's database of samples. 1 sample has details, , several samples can collated collatedsample, , details can collated in collateddetail. so, 1 collatedsample has many collateddetails, , starts many samples, each of has many details. collateddetail has many details too. it's "nice" square.

my approach adding records thus:

var sample = new sample(); var detail = new detail(); sample.details.add(detail); // suppose add bit more meat these entities...  var collatedsample = new collatedsample(); var collateddetail = new collateddetail(); collatedsample.samples.add(sample); collatedsample.collateddetails.add(collateddetail); collateddetail.details.add(detail);  context.collatedsamples.addobject(collatedsample); context.savechanges(); 

so i've added elements eachother, , added detail both sample , collateddetail. on savechanges, update exception jolly message:

unable determine principal end of 'samplingmodel.fk_detail_collateddetailid' relationship. multiple added entities may have same primary key.

what think might happening there attempt record detail entity before collateddetail recorded. detail table, 2 relationships, 1 causing trouble (not adding them either sample nor collateddetail confirms it). perhaps there way specify order of insertion? tried reverse, set parent instead of using .add() on children collections, same result. otherwise, how make sort of 2-pronged insertion in 1 shot?

edit: tl;dr:

enter image description here

i found workaround: removed foreign key between detail , collateddetail @jaderdiag suggested, , reference field collateddetail table. entity framework creates partial classes, easy create other partial classes both entities , manually join them. much slower, suspect, provides same fluidity in later exploitation entities have provided foreign key.

this workaround, , not solution looking for. vote down if could.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -