如何正确使用两个多对一关系?
public class Man
{
public Man()
{
}
[Key]
public int idMan; { get; set; }
[Required]
public string ManName { get; set; }
public virtual List<Computer> listComputersBought { get; set; }
public virtual List<Computer> listComputersSold { get; set; }
}
public class Computer
{
public Computer()
{
}
[Key]
public int idComputer{ get; set; }
public string ComputerName {get;set;}
[ForeignKey("ManVendor")]
public int idManVendor{ get; set; }
public virtual Man ManVendor { get; set; }
[ForeignKey("ManBuyer")]
public int idManBuyer{ get; set; }
public virtual Man ManBuyer { get; set; }
}
我正试图建立两个多对一的关系。这样做,我收到了以下错误:
“Project1.Models.Computer”类型的属性“idManBuyer”上的ForeignKeyAttribute无效。在依赖类型“Project1.Models.Computer”上找不到导航属性“ManBuyer”。 Name值应该是有效的导航属性名称。
请问如何解决这个问题?
有什么好主意吗?
非常感谢!
答案 0 :(得分:0)
做这样的事情:
public class Man
{
public Man()
{
}
[Key]
public int idMan; { get; set; }
[Required]
public string ManName { get; set; }
public virtual ICollection<Computer> listComputersBought { get; set; }
public virtual ICollection<Computer> listComputersSold { get; set; }
}
public class Computer
{
public Computer()
{
}
[Key]
public int idComputer{ get; set; }
public string ComputerName {get;set;}
[ForeignKey("ManBuyer")]
public int idMan{ get; set; }
public virtual Man ManBuyer { get; set; }
}