我在实体框架中使用代码优先方法。我得到了以下错误。
列名'Party_ID‘无效。exec sp_executesql N‘Invalid.PartyCellPhone值(@0,NULL,NULL,@1),N'@0 int,@1 nvarchar(max) ',@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方法时,它将显示此错误。我怎样才能解决这个问题?
发布于 2017-06-02 11:08:10
外键属性的代码第一约定要求您的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; }
https://stackoverflow.com/questions/44326286
复制相似问题