加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

entity-framework – 实体类型没有键定义的EF6

发布时间:2020-12-16 07:10:55 所属栏目:asp.Net 来源:网络整理
导读:这是我的代码,虽然我已经改进了关键属性,但仍然存在问题. public class Contacts{ [Key] public int ContactId { get; set; } public string Name { get; set; } public string Address { get; set; } public string City { get; set; } public string State
这是我的代码,虽然我已经改进了关键属性,但仍然存在问题.

public class Contacts
{
    [Key]
    public int ContactId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
}

我得到的错误是:

Entity Type ‘Contacts’ has no key defined. Define the key for this entity type.
Entity type: EntitySet ‘Contacts’ is based on type ‘Contacts’ that has no key defined

解决方法

如果您使用的是EF Code First(未在问题中指定),则需要将ContactId属性名称更改为ContactsId以匹配ClassName Id的约定,以便为Contacts实体类型定义键.

请参阅MSDN Code First Conventions:http://msdn.microsoft.com/en-us/data/jj679962.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读