实体类型没有定义密钥
问题描述:
另一个'实体类型'x'没有定义密钥'的问题,但我在属性上设置了[Key]
属性,所以我有点困惑。实体类型没有定义密钥
这里是我的实体和上下文类:
namespace DoctorDB.Models
{
public class Doctor
{
[Key]
public string GMCNumber;
[Required]
public string givenName;
[Required]
public string familyName;
public string MDUNumber;
public DateTime MDUExpiry;
public string MDUCover;
}
public class DoctorContext : DbContext
{
public DbSet<Doctor> Doctors { get; set; }
}
}
当我去创造我的控制器,我选择与使用该实体和语境实体框架方法创建它:
,我得到这个错误:
我唯一的想法就是你是否无法成功地在字符串属性上使用[Key]。如果你现在还不够公平,我会努力工作的,但如果有人能以这种或那种方式证实这一点,我会很感激。
答
您需要将GMCNumber
更改为属性而非字段。
答
为了帮助澄清,这条线:
public string GMCNumber;
需求,成为:
答
我遇到了同样的错误消息时我已经定义的属性为私有。
答
今天面对类似的问题后,我遇到了这个帖子。问题在于我在将[Key]属性添加到我的模型并且没有编译之后尝试创建脚手架。一旦我编写了[Key]属性,脚手架就产生了。
答
当我使用不是[Key]字符串的属性时,我得到相同的错误。
这里有一个摄制:
public class Doctor
{
[Key]
public Identity ID { get; set; }
public string Address { get; set; }
}
public class Identity
{
[Key]
public string GivenName { get; set; }
[Key]
public string FamilyName { get; set; }
}
在构建时出现的错误,而是当网站负载:
An exception of type 'System.InvalidOperationException' occurred in System.Web.OData.dll but was not handled in user code
Additional information: The entity 'Doctor' does not have a key defined.
我的模型包含在它的属性,而不是字段。和我得到相同的错误。建议? – Zeeshan 2013-12-13 07:32:02
我建议你问你自己的问题发布你的代码和完整的错误信息。 – codingbadger 2013-12-13 07:48:13