EF 4.1 EntityType没有密钥-复合 [英] EF 4.1 EntityType has no key - composite

查看:84
本文介绍了EF 4.1 EntityType没有密钥-复合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚使用NuGet升级到最新的EF 4.1代码,现在我收到有关映射的错误.

I have just upgraded to the latest EF 4.1 code-first using NuGet and now I am receiving an error regarding my mapping.

我有这堂课

public class UserApplication
{
    [Key, Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }

    [Key, Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ApplicationId { get; set; }

    [ForeignKey("ApplicationId")]
    public virtual Application Application { get; set; }

    public DateTime SubscribedDate { get; set; }
}

我收到此错误:

System.Data.Edm.EdmEntityType: : EntityType 'UserApplication' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet 'UserApplications' is based on type 'UserApplication' that has no keys defined.

但是我可以像这样使用Fluent API使其工作

I can however make it work using the Fluent API like this

modelBuilder.Entity<UserApplication>().HasKey(obj => new {obj.UserId, obj.ApplicationId });

为什么EF不使用我通过注释定义的组合键? 我相当确定这曾经与较旧的EF 4.1配合使用.

How come EF doesn't pick up the composite key that I have defined using annotations? I am quite certain that this used to work with an older EF 4.1.

推荐答案

我已经解决了这个问题.我刚刚添加了其他关键列:

I've solved the issue. I've just added additional key columns:

public class AlbumUser
{
    public Album Album
    {
        get;
        set;
    }

    public IdentityUser User
    {
        get;
        set;
    }

    [Key]
    [Column(Order = 0)]
    [StringLength(128)]
    public string UserId
    {
        get;
        set;
    }

    [Key]
    [Column(Order = 1)]
    public int AlbumId
    {
        get;
        set;
    }
}

这篇关于EF 4.1 EntityType没有密钥-复合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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