简体   繁体   English

EF 6代码优先错误:EntityType没有定义键

[英]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.

相关问题 Ef 代码第一个错误:EntityType: 'ModelName' has no key defined - Ef code first error: EntityType: 'ModelName' has no key defined 实体框架代码第一个错误 - EntityType 'Variable' 未定义键 - Entity Framework Code First Error - EntityType 'Variable' has no key defined EntityType 没有键定义错误 - EntityType has no key defined error 实体框架代码优先,错误:“ EntityType'OfferSequence'没有定义键” - Entity Framework code-first, error: “EntityType 'OfferSequence' has no key defined” 实体框架6.1代码首次迁移错误:EntityType'IdentityUserRole'没有定义键 - Entity Framework 6.1 Code First migration error: EntityType 'IdentityUserRole' has no key defined 代码优先-EntityType'编译器结果'尚未定义键。 定义此EntityType的键 - Code First - EntityType 'Compiler Results' has no key defined. Define the key for this EntityType EntityType没有定义键 - EntityType has no key defined EntityType''IdentityUserRole'没有键定义错误 - EntityType ''IdentityUserRole' has no key defined error 验证错误:EntityType没有定义键 - Validation error: EntityType has no key defined 代码优先:EntityType 'xxxx' 没有定义键。 添加 n 对 n 关系时定义此 EntityType 的键 - Code first: EntityType 'xxxx' has no key defined. Define the key for this EntityType when adding n-to-n relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM