实体类型没有键定义EF6 [英] Entity type has no key defined EF6

查看:281
本文介绍了实体类型没有键定义EF6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,尽管我已经优化了key属性,但是仍然存在问题.

Here is my code although I already have refined the key attribute but still there is a problem.

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; }
}

我得到的错误是:

实体类型联系人"没有定义键.定义此实体类型的键.
实体类型:EntitySet联系人"基于未定义键的类型联系人"

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 实体类型定义键.

If you are using EF Code First (not specified in your question), you will need to change the ContactId property name to ContactsId to match the convention of ClassName + Id in order to define the key for your Contacts entity type.

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

See the MSDN Code First Conventions: http://msdn.microsoft.com/en-us/data/jj679962.aspx

这篇关于实体类型没有键定义EF6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆