2017-06-02 90 views
0

我在实体框架中使用第一种方法。我收到以下错误。EF:无效的列名'Party_ID'

无效的列名'Party_ID'。 exec sp_executesql N'INSERT [dbo]。[PartyCellPhone]([ID],[Party_ID],[CountryCode],[Number]) VALUES(@ 0,NULL,NULL,@ 1)',N'@ 0 int ,@ 1个为nvarchar(最大) ”,@ 0 = 13,@ 1 = N'1234546877'

的InnerException = { “无效的列名称Party_ID'。”}

public abstract class PartyContact:BaseModel 
    { 
     public PartyContact() 
     { 

     } 

     [Key] 
     public int ID { get; set; } 
     //public Enums.ContactType ContactType { get; set; } 
     //public Enums.ContactUsage ContactUsage { get; set; } 

     public abstract Enums.ContactMethod ContactMethod { get; } 
     public int ContactTypeID { get; set; } 
     public int ContactUsageID { get; set; } 
     public int ContactMethodID { get; set; } 
     public int PartyID { get; set; } 
     public abstract bool Validate(); 
     [ForeignKey("PartyID")] 
     public virtual Party Party { get; set; } 
    } 

public class CellPhone : PartyContact, ICellPhone 
    { 
     public string CountryCode { get; set; } 
     public string Number { get; set; } 

     public override Enums.ContactMethod ContactMethod 
     { 
     get 
      { 
      return Enums.ContactMethod.Cell; 
      } 
     } 
    /// <summary> 
    /// Initializes a new instance of the ContactNumber class with default values. 
    /// </summary> 
    public CellPhone() 
    { 
    } 

    } 

当我调用save方法,它向我显示这个错误。我该如何解决这个问题?

+0

这里没有EF专家,但是您是否确认名称是DB上的Party_ID?你的参考文献有PartyId –

回答

0

代码优先约定外键属性需要您PartyContact类,以便您Party类的匹配性。我猜你有Party_ID作为你的Party类的主键。

做这个

public int Party_ID { get; set; } 
public virtual Party Party { get; set; } 

还是这个,如果由于某种原因,你想比你Party类的ID不同的命名外键。

[ForeignKey("Party_ID")] 
public int PartyID { get;set; } 
public virtual Party Party { get; set; }