实体框架6错误 - 未定义任何键

时间:2014-07-10 02:22:08

标签: c# entity-framework-6

我在编译器中收到以下错误:

MySite.DAL.ContentArticleSubdivision: : EntityType 'ContentArticleSubdivision' has no key defined. Define the key for this EntityType.
ContentArticleSubdivision: EntityType: EntitySet 'ContentArticleSubdivision' is based on type 'ContentArticleSubdivision' that has no keys defined.

但是,我已经定义了ContentArticleSubdivision:

public partial class ContentArticleSubdivision
{
    public long ContentArticleId { get; set; }
    public int SubdivisionId { get; set; }

    public virtual ICollection<Subdivision> Subdivisions { get; set; }
}

其中ContentArticleId是返回&#39; ContentArticle&#39;的外键,其定义如下:

public partial class ContentArticle
{
    [KeyAttribute] /* needed for FreeTextSearch class - if used */
    public long Id { get; set; }

我的理解是因为我已经以<LinkedEntityTypeName><PrimaryKeyField>的形式命名了我的外键属性,即我拥有属性ContentArticleId,其中链接到了&#39; Id&#39;正如“内容文章”中所定义的那样。模型,EF将理解ContentArticleId应该是外键。我错过了什么?

1 个答案:

答案 0 :(得分:2)

实体需要在任何表上使用主键,在错误中提到的特定表中添加一个应该解决问题。