2014-04-15 49 views
0

如何正確使用兩個多對一的關係?如何正確使用兩個多對一的關係?

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; } 
    } 

我試圖讓兩個多對一的關係。這樣做,我得到了波紋管錯誤:

The ForeignKeyAttribute on property 'idManBuyer' on type 'Project1.Models.Computer' is not valid. The navigation property 'ManBuyer' was not found on the dependent type 'Project1.Models.Computer'. The Name value should be a valid navigation property name.

如何解決此問題,請?

任何絕妙的想法?

非常感謝!

回答

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; } 
    } 
相關問題