So I have a model that has a Salesman
and a 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.
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.
The Region populates fine in the Salesman.Region
property.
Stuff tried and in the project
ICollection
to ISet
I am possibly thinking that it might have to do with a naming convention like Salesman -> Salesmen as opposed to Salesman -> Salesmans
Thanks in advance.
So I solved it by adding an attribute to the ICollection<Salesman> Salesmen
property.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.