EF 4.1 EntityType 没有键 - 复合

发布于 2024-11-28 13:09:38 字数 1023 浏览 3 评论 0原文

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

我有这个类

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 使其工作

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

为什么 EF 没有拾取我使用注释定义的复合键? 我非常确定这曾经适用于较旧的 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.

I have this class

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; }
}

And I am getting this error:

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.

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-12-05 13:09:39

适用于实体框架 6.1.3。我还添加了一个基本的 Application 类,但按原样使用了 UserApplication(DatabaseGeneratedOption 除外)。生成了以下 SQL:

CREATE TABLE [dbo].[UserApplication] (
    [UserId] [int] NOT NULL,
    [ApplicationId] [int] NOT NULL,
    [SubscribedDate] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.UserApplication] PRIMARY KEY ([UserId], [ApplicationId])
)


CREATE TABLE [dbo].[Application] (
    [ApplicationId] [int] NOT NULL IDENTITY,
    [Name] [nvarchar](max),
    CONSTRAINT [PK_dbo.Application] PRIMARY KEY ([ApplicationId])
)

Works with Entity Framework 6.1.3. I also added a basic Application class, but used the UserApplication as is (except for DatabaseGeneratedOption. This SQL was generated:

CREATE TABLE [dbo].[UserApplication] (
    [UserId] [int] NOT NULL,
    [ApplicationId] [int] NOT NULL,
    [SubscribedDate] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.UserApplication] PRIMARY KEY ([UserId], [ApplicationId])
)


CREATE TABLE [dbo].[Application] (
    [ApplicationId] [int] NOT NULL IDENTITY,
    [Name] [nvarchar](max),
    CONSTRAINT [PK_dbo.Application] PRIMARY KEY ([ApplicationId])
)
梨涡 2024-12-05 13:09:38

我已经解决了这个问题。我刚刚添加了额外的关键列:

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文