[英]EF CodeFirst - Adding ICollection Navigation Properties
I'm trying to figure out, how to implement navigation properties to my entities... But my navigation properties is always null: 我试图找出,如何实现导航属性到我的实体...但我的导航属性始终为null:
I've set up two entities: 我已经建立了两个实体:
Entity 1 contains this lines: 实体1包含以下行:
public int Id { get; set; }
public ICollection<BestellterArtikel> BestellteArtikel { get; set; }
My second entity looks like this: 我的第二个实体看起来像这样:
public int Id { get; set; }
public int BestellungId { get; set; }
public Bestellung BestellteArtikel { get; set; }
Further more I included this line to my overwritten OnModelCreating-Method: 此外,我将此行包含在我覆盖的OnModelCreating-Method中:
modelBuilder.Entity<Bestellung>().HasMany(e => e.BestellteArtikel).WithRequired(e => e.Bestellung);
What have I done wrong? 我做错了什么? Have I forgotten something important?
我忘记了重要的事吗? And does it has to be so complex?
它必须如此复杂吗? Do I have to add a line in my overwritten method for each property?
我是否必须在每个属性的覆盖方法中添加一行?
Here is my solution : 这是我的解决方案:
Entity 1: 实体1:
public virtual ICollection<BestellterArtikel> BestellteArtikel { get; set; }
Entity 2: 实体2:
public virtual Bestellung BestellteArtikel { get; set; }
Edited: 编辑:
also you have to revise your mapping: 你也必须修改你的映射:
modelBuilder.Entity<Bestellung>().HasMany(e => e.BestellteArtikel).WithRequired(e => e.BestellteArtikel );
Instead of referring to BestellteArtikel property, you referred to type! 不是指BestellteArtikel属性,而是提到类型!
What do you mean by "always null"? 你总是说“永远”是什么意思?
If you are talking about null values when you try to read them from DB, 如果您在尝试从DB中读取空值时谈论空值,
then remember that you need to eagerly load the navigation properties when you query the context, or use EF lazy-loading. 然后记住,当您查询上下文或使用EF延迟加载时,您需要急切地加载导航属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.