简体   繁体   English

EF CodeFirst - 添加ICollection导航属性

[英]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延迟加载时,您需要急切地加载导航属性。

Read this for more information. 阅读本文以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF codefirst:我应该初始化导航属性吗? - EF codefirst : Should I initialize navigation properties? EF 4.3和CodeFirst:一对多导航属性在没有实体代理的情况下加载为空值 - EF 4.3 & CodeFirst: One-to-many navigation properties loads as nulls without entity proxy EF Core 2.0 Identity - 添加导航属性 - EF Core 2.0 Identity - Adding navigation properties 添加ICollection导航属性后,EF重新添加FK列 - EF re-adding FK columns after adding ICollection navigation property 在导航属性中添加新项目会导致“集合导航属性必须&gt;实现目标类型的ICollection &lt;&gt;”错误 - Adding a new item into navigation property causes “Collection navigation properties must > implement ICollection<> of the target type” error 忽略EF CodeFirst中所有已实现的接口属性 - Ignoring all implemented interface properties in EF CodeFirst DbContext初始化ICollection导航属性,但不初始化其他属性 - DbContext Initializing ICollection navigation properties but not others 在EF7中添加相同类型的多个导航属性 - Adding multiple navigation properties of the same type in EF7 具有 EF 导航属性的 Automapper - Automapper with EF Navigation Properties EF导航属性不起作用? - EF Navigation Properties not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM