[英]DbContext Initializing ICollection navigation properties but not others
Having 有
public class ClassOne
{
[Key]
public virtual int ClassOneID { get; set; }
[InverseProperty("ClassOne")]
public virtual ICollection<ClassTwo> ClassTwoCollection { get; set; }
}
public class ClassTwo
{
[Key]
public virtual int ClassTwoID { get; set; }
[ForeignKey("ClassOne")]
public virtual int ClassOneID { get; set; }
[InverseProperty("ClassTwoCollection")]
public virtual ClassOne ClassOne { get; set; }
[InverseProperty("ClassTwo")]
public virtual ICollection<ClassThree> ClassThreeCollection { get; set; }
[InverseProperty("ClassTwo")]
public virtual ClassFour ClassFour { get; set; }
[InverseProperty("ClassTwoPrimary")]
public virtual ICollection<ClassFour> ClassFourCollectionAsPrimary { get; set; }
}
public class ClassThree
{
[Key]
public virtual int ClassThreeID { get; set; }
[ForeignKey("ClassTwo")]
public virtual int ClassTwoID { get; set; }
[InverseProperty("ClassThreeCollection")]
public virtual ClassTwo ClassTwo { get; set; }
}
public class ClassFour
{
[Key]
[ForeignKey("ClassTwo")]
public virtual int ClassTwoID { get; set; }
[ForeignKey("ClassTwoPrimary")]
public virtual int ClassTwoPrimaryID { get; set; }
[InverseProperty("ClassFour")]
public virtual ClassTwo ClassTwo { get; set; }
[InverseProperty("ClassFourCollectionAsPrimary")]
public virtual ClassTwo ClassTwoPrimary { get; set; }
}
If I create a ClassOne: myDbCntxt.ClassOneSet.Create();
如果我创建一个ClassOne: myDbCntxt.ClassOneSet.Create();
the ClassTwoCollection is initialized. ClassTwoCollection被初始化。
If I create a ClassTwo: myDbCntxt.ClassTwoSet.Create();
如果我创建一个ClassTwo: myDbCntxt.ClassTwoSet.Create();
the ClassThreeCollection and ClassFourCollectionAsPrimary are null. ClassThreeCollection和ClassFourCollectionAsPrimary为空。
WHY? 为什么?
Proxy creation is enabled and I can see the class name generated an they are proxy classes. 启用了代理创建,我可以看到生成的类名,它们是代理类。 I know I'm over decorating my properties, but that is not the question today. 我知道我已经在装饰我的房屋了,但这不是今天的问题。
All I would like to understand is WHY for ClassTwo the Collection properties are not initialized when created from DbContext. 我只想了解ClassTwo的原因,所以从DbContext创建Collection属性时不会对其进行初始化。
Thanks 谢谢
UPDATE : 更新 :
The problem was actually having another member NOT declared as virtual in ClassTwo. 问题实际上是在ClassTwo中没有将另一个成员声明为虚拟成员。 I guess there is the rule that all your public members should be virtual for the collection properties to be initialized in the proxies. 我猜有一个规则,您的所有公共成员都应该是虚拟的,以便在代理中初始化集合属性。
Found the problem. 找到了问题。
The problem was actually having another member NOT declared as virtual in ClassTwo. 问题实际上是在ClassTwo中没有将另一个成员声明为虚拟成员。 I guess there is the rule that all your public members should be virtual for the collection properties to be initialized in the proxies. 我猜有一个规则,您的所有公共成员都应该是虚拟的,以便在代理中初始化集合属性。
I really don't understand why this behavior. 我真的不明白为什么这种行为。 What if you don't want an Int32 property to be overriden at all in your proxies. 如果您不希望在代理中完全覆盖Int32属性,该怎么办。 This shouldn't affect the default behavior for virtual navigation properties. 这不应影响虚拟导航属性的默认行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.