2013-02-12 99 views
0

我有2表合同和帐户在我的数据库中,我只想修改合同表,如果帐户代码是在帐户表中。ASP.NET MVC,CRUD 2表1与外键

这些表格已经在数据库中创建。我有2个实体在我的MVC应用程序:

合同实体:

public class Contract 
    { 
     [Key] 
     [HiddenInput(DisplayValue = false)] 
     public int ContractID { get; set; } 

     [Display(Name = "Account Code")] 
     //[ForeignKey("AccountCode")] 
     public string AccountCode { get; set; } 
     //public virtual Account AccountCode { get; set; } 

     [Display(Name = "Product Code")] 
     public string ProductCode { get; set; } 

    } 

在我的帐号实体:

public class Accounts 
    { 
     [Key] 
     public string AccountCode { get; set; } 
     public string CompanyName { get; set; } 
     public string CompanyNumber { get; set; } 
     public string VATNumber { get; set; } 
     public string Telephone { get; set; } 
     public string AddressLine1 { get; set; } 
     public string AddressLine2 { get; set; } 
     public string City { get; set; } 
     public string County { get; set; } 
     public string PostCode { get; set; } 
     public string Country { get; set; } 

     //public virtual ICollection<Contract> Contract { get; set; } 
    } 

,因为我让他们注释掉的CRUD目前正在无外键。

我在合同表上的数据库中有一个外键(AccountCode)链接到accounts表。我如何在上面使用我的实体的mvc应用程序上实现该功能? 我已阅读谷歌,并尝试上述评论代码没有任何成功。

在此先感谢您的帮助

回答

0

这会为你做它

public class Contract 
     { 
      [Key] 
      [HiddenInput(DisplayValue = false)] 
      public int ContractID { get; set; } 
      public virtual Account Account{ get; set; } 
      [Display(Name = "Product Code")] 
      public string ProductCode { get; set; } 
      public string AccountAccountCode {get;set;} 
//automatically treated as foreign key of Account[Notice ClassNameKey Format] 

    }