![](/img/trans.png)
[英]Two many-to-one table relationships and insertion with Entity Framework
[英]How to properly use two Many-to-one relationships?
如何正確使用兩個多對一關系?
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值應為有效的導航屬性名稱。
請如何解決此問題?
有什么好主意嗎?
非常感謝!
做這樣的事情:
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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.