[英]Referenced objects are not being populated in EF6 collection
So I have a model that has a Salesman
and a Region
. 因此,我有一个具有Salesman
和Region
的模型。 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 在项目中尝试过的东西
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.