简体   繁体   English

DbContext初始化ICollection导航属性,但不初始化其他属性

[英]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.

相关问题 EF CodeFirst - 添加ICollection导航属性 - EF CodeFirst - Adding ICollection Navigation Properties DbContext需要手动加载导航属性 - DbContext requires manual load of navigation properties 实体框架ICollection导航属性仅在测试时为null - Entity Framework ICollection Navigation Properties null only when testing 在导航属性中添加新项目会导致“集合导航属性必须&gt;实现目标类型的ICollection &lt;&gt;”错误 - Adding a new item into navigation property causes “Collection navigation properties must > implement ICollection<> of the target type” error 如何从导航属性中查询 EF Core dbcontext 的特定属性? - How to query EF Core dbcontext for specific attributes from navigation properties? 实体框架导航属性未加载直到新的DbContext已建立 - Entity Framework Navigation Properties not loading til new DbContext is established 在ICollection上包含导航道具 - Include navigation prop on ICollection ICollection类型的访问导航属性 - Access Navigation Property of type ICollection 获取类型为ICollection的属性<BaseEntity> - Get properties that are of type ICollection<BaseEntity> 为什么在 EF6 中调用 dbContext.Save 后会重置导航属性 - Why are navigation properties reset after calling dbContext.Save in EF6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM