简体   繁体   English

EF6集合中未填充引用的对象

[英]Referenced objects are not being populated in EF6 collection

So I have a model that has a Salesman and a Region . 因此,我有一个具有SalesmanRegion的模型。 The salesman belongs to a region. 推销员属于一个区域。

Salesman.cs 推销员

public class Salesman : Employee
{
    public Salesman()
    {
        this.Invoices = new List<Invoice>();
    }

    public int? RegionId { get; set; }

    [ForeignKey("RegionId")]
    public virtual Region Region { get; set; }

    public virtual ICollection<Invoice> Invoices { get; set; }
}

Salesman inherits Employee with stores basic name, address, etc data - which is not relevant. Salesman使用存储的基本名称,地址等数据继承Employee这是不相关的。

Region.cs Region.cs

public class Region
{
    public Region()
    {
        this.Salesmen = new List<Salesman>();
    }

    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public int? SalesManagerId { get; set; }

    [ForeignKey("SalesManagerId")]
    public virtual Salesman SalesManager { get; set; }

    public virtual ICollection<Salesman> Salesmen { get; set; }

}

The issue I am having is that Region.Salesmen is not being filled by EF like it has in other projects. 我遇到的问题是,与其他项目一样,EF没有填补Region.Salesmen

The Region populates fine in the Salesman.Region property. Region在Salesman.Region属性中填充良好。

Stuff tried and in the project 在项目中尝试过的东西

  • Lazy-loading is on (I have explicitly enabled it) 延迟加载已启用(我已明确启用它)
  • Renaming property names ie. 重命名属性名称,即。 Salesmen -> Salesmans 推销员->推销员
  • The database has the correct schema 数据库具有正确的架构
  • Changing ICollection to ISet ICollection更改为ISet

I am possibly thinking that it might have to do with a naming convention like Salesman -> Salesmen as opposed to Salesman -> Salesmans 我可能认为这可能与诸如Salesman-> Salesman而不是Salesman-> Salesmans这样的命名约定有关

Thanks in advance. 提前致谢。

So I solved it by adding an attribute to the ICollection<Salesman> Salesmen property. 因此,我通过向ICollection<Salesman> Salesmen属性添加属性来解决了该问题。

Now my model looks like this... 现在我的模型看起来像这样...

public class Region
{
    public Region()
    {
        this.Salesmen = new List<Salesman>();
    }

    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public int? SalesManagerId { get; set; }

    [ForeignKey("SalesManagerId")]
    public virtual Salesman SalesManager { get; set; }

    [InverseProperty("Region")]
    public virtual ICollection<Salesman> Salesmen { get; set; }

}

Don't ask me why it works, and why EF couldn't link it up without a little help.. but it does. 不要问我为什么它起作用,以及为什么EF在没有一点帮助的情况下无法将其链接..但是它确实起作用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM