[英]EF 6 Code First Error: EntityType has no key defined
I have defined a class that holds the createby,createdate,modifyby and modifydate properties which is inherited by other classes { so that i don't have to create the field in all the entities that have those field}. 我定义了一个类,该类包含由其他类继承的createby,createdate,modifyby和Modifydate属性{这样,我就不必在具有这些字段的所有实体中都创建该字段}。 My code is
我的代码是
public class SMSModelBaseClass
{
[Required]
[StringLength(50)]
[Column(TypeName ="varchar")]
[ScaffoldColumn(false)]
public string CreateById { get; set; }
[Required]
[ScaffoldColumn(false)]
public DateTime CreateDate { get; set; }
[Required]
[StringLength(50)]
[ScaffoldColumn(false)]
[Column(TypeName = "varchar")]
public string ModifyById { get; set; }
[Required]
[ScaffoldColumn(false)]
public DateTime ModifyDate { get; set; }
[ForeignKey("CreateById")]
public virtual ApplicationUser CreateBy { get; set; }
[ForeignKey("ModifyById")]
public virtual ApplicationUser ModifyBy { get; set; }
}
Am getting the subject error. 正在收到主题错误。 I know i could introduce a primary key for the same but it will get inherited by the other classes, something that i dont want.
我知道我可以为它引入一个主键,但是它将被其他类继承,这是我不想要的。
Is it possible either using fluent api or data annotations to bypass this error? 是否可以使用流利的api或数据注释来绕过此错误?
With EntityFramework, each entity needs a key property. 使用EntityFramework,每个实体都需要一个关键属性。
Typically this is an Id
column. 通常,这是一个
Id
列。
It could be a composite key, but given your model, I would suggest an Id
property on SMSModelBaseClass
它可能是一个组合键,但是考虑到您的模型,我建议在
SMSModelBaseClass
上使用Id
属性
As a side note, I believe SMSModelBaseClass
should also be abstract
if you are using it as a base class, and not instantiating it directly 附带说明一下,如果您将
SMSModelBaseClass
用作基类,而不是直接实例化它,我相信SMSModelBaseClass
也应该是abstract
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.