在早期的EF中,在 1-parent:many-child 关系中,如果在子项上设置了父引用,则子项也会添加到父项的子项集合中。这在代码中是不一样的?
使用以下示例,一个父条目可以有多个子 FindKey :
EfTestModel eft = new EfTestModel();
Entry en = eft.Entries.First(); //Get an entry to work with
int cnt1 = en.FindKeys.Count(); //=0 en's FindKeys collection is empty
FindKey fk = eft.FindKeys.Create(); //Create new entity with proxies
fk.Entry = en; //Set the parent relation
//other fields updated here
int cnt2 = en.FindKeys.Count(); //=0 en's FindKeys collection still empty
eft.FindKeys.Add(fk); //Add the new FindKey to the context
int cnt3 = en.FindKeys.Count(); //=1 fk now added to FindKeys collection
为什么cnt2 = 0而不是1,因为设置了父关系? cnt3 = 1:为什么将它添加到上下文中会填充 FindKey 的集合?是否有手动方式来触发关系魔法?
如果我正在更新而不是添加新的 FindKey (因此无法调用 .Add(fk))是否有另一种方式来填充关系和因此,确保所有关系都正确吗?