首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >EF:无效列名“Party_ID”

EF:无效列名“Party_ID”
EN

Stack Overflow用户
提问于 2017-06-02 10:07:14
回答 1查看 214关注 0票数 0

我在实体框架中使用代码优先方法。我得到了以下错误。

列名'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’”}。

代码语言:javascript
运行
复制
 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方法时,它将显示此错误。我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2017-06-02 11:08:10

外键属性的代码第一约定要求您的PartyContact类具有Party类的匹配属性。我猜您将Party_ID作为Party类的主键。

做这个

代码语言:javascript
运行
复制
public int Party_ID { get; set; }
public virtual Party Party { get; set; }

如果出于某种原因,您希望将外键命名为与Party类的ID不同的名称。

代码语言:javascript
运行
复制
[ForeignKey("Party_ID")]
public int PartyID { get;set; }
public virtual Party Party { get; set; }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44326286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档