[英]Two many-to-one table relationships and insertion with Entity Framework
I have a model similar to this: 我有一个与此类似的模型:
Context: The idea is that it's a database of samples. 上下文:这个想法是它是一个样本数据库。 One sample has details, and several samples can be collated together into a CollatedSample, and the details can be collated together as well in a CollatedDetail. 一个样本具有详细信息,可以将多个样本整理到CollatedSample中,也可以在CollatedDetail中将详细信息整理到一起。 So, one CollatedSample has many CollatedDetails, and starts from many Samples, each of which has many Details. 因此,一个CollatedSample具有许多CollatedDetails,并且从许多Samples开始,每个Samples具有许多Details。 A CollatedDetail has many Details too. 一个CollatedDetail也有许多Details。 It's a "nice" square. 这是一个“不错”的广场。
My approach to adding records is thus: 因此,我添加记录的方法是:
var sample = new Sample();
var detail = new Detail();
sample.Details.Add(detail);
// suppose I add a bit more meat to 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 all elements to eachother, and added Detail to both Sample and CollatedDetail. 因此,我将所有元素都添加到了彼此,并在Sample和CollatedDetail中都添加了Detail。 On SaveChanges, I get an Update Exception with the jolly message: 在SaveChanges上,我得到了带有异常消息的Update Exception:
Unable to determine the principal end of the 'SamplingModel.FK_Detail_CollatedDetailId' relationship. 无法确定“ SamplingModel.FK_Detail_CollatedDetailId”关系的主要结尾。 Multiple added entities may have the same primary key. 多个添加的实体可能具有相同的主键。
What I think might really be happening is there is an attempt to record the Detail entity before the CollatedDetail is recorded. 我认为可能真正发生的是在记录CollatedDetail之前尝试记录Detail实体。 That Detail table, with its two relationships, is the one causing the trouble (not adding them to either Sample nor CollatedDetail confirms it). 那个具有两个关系的详细信息表是引起麻烦的一个(没有将它们添加到Sample或CollatedDetail中也无法确认)。 Perhaps there is a way to specify the order of insertion? 也许有一种方法可以指定插入顺序? I also tried the reverse, to set the parent instead of using .Add()
on children collections, with the same result. 我还尝试了相反的操作,设置了父级,而不是对子级集合使用.Add()
,结果相同。 Otherwise, how do I make this sort of 2-pronged insertion in one shot? 否则, 我如何一次完成两叉插入?
EDIT: tl;dr: 编辑:tl;博士:
I found a workaround: I removed the Foreign Key between Detail and CollatedDetail like @JaderDiag suggested, and the reference field from the CollatedDetail table. 我找到了一种解决方法:删除了Detail和CollatedDetail之间的外键(如@JaderDiag建议),并从CollatedDetail表中删除了引用字段。 Entity Framework creates partial classes, so it was easy to create other partial classes for both entities and manually join them. 实体框架创建子类,因此很容易为两个实体创建其他子类并手动将它们联接。 This will be much much slower, I suspect, but it provides the same fluidity in later exploitation as the entities would have provided with the foreign key. 我怀疑这会慢得多,但是在以后的开发中,它提供的流动性与实体将提供外键的流动性相同。
This is a workaround, and definitely not the solution I was looking for. 这是一种解决方法,绝对不是我要找的解决方案。 Would vote this down if I could. 如果可以的话,我会否决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.