EF:列名称无效' Party_ID'

时间:2017-06-02 10:07:14

标签: asp.net-mvc entity-framework c#-4.0

我在Entity Framework中使用代码优先方法。我收到以下错误。

  

列名称无效' Party_ID'。 exec sp_executesql N' INSERT   [dbo]。[PartyCellPhone]([ID],[Party_ID],[CountryCode],[Number])   VALUES(@ 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方法时,它显示我这个错误。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

外键属性的Code-First约定要求您的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; }