【问题标题】:User in Entity type MVC5 EF6实体类型 MVC5 EF6 中的用户
【发布时间】:2013-11-23 16:09:48
【问题描述】:

我在 MVC5 中创建了一个类,我想要一个内容的主要所有者,然后我想要一些内容的编辑器:

public class Content
{
    public int ID { get; set; }
    public IdentityUser Owner { get; set; }
    public ICollection<IdentityUser> Editors { get; set; }
    public string Title{ get; set; }
    public string Body { get; set; }
}

在数据库上下文中,我有以下代码:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Content>()
        .HasOptional(c => c.Editors)
        .WithRequired()
        .WillCascadeOnDelete();

    modelBuilder.Entity<Content>()
        .HasRequired(c => c.Owner)
        .WithOptional()
        .WillCascadeOnDelete();
}

我希望 Microsoft 以这样的方式实现 IdentityUser 对象,它可以用于其他实体类型,所以我可能做错了什么,因为当我尝试为 Content 实体创建控制器时输入,我收到以下错误:

EntityType 'IdentityUserLogin' has no key defined
EntityType 'IdentityUserRole' has no key defined
EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no key defined
EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no key defined

我还尝试将以下代码添加到ApplicationDbContext,如本问题Map tables using fluent api in asp.net MVC5 EF6?中所述:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}

我一定是做错了什么。请告诉我如何处理我的 Content EntityType 中的用户。

【问题讨论】:

  • 您的 Content 类似乎缺少外键 ID。
  • 使用 EntityFramework 时,您不需要外键 ID 字段。您只需将属性设置为关联类型,EF 会完成其余的工作(例如在数据库中存储外键 ID)。

标签: c# asp.net entity-framework asp.net-mvc-5 asp.net-identity


【解决方案1】:

我有同样的问题,我发现我没有打电话

 base.OnModelCreating(modelBuilder);

在我的 DbModel 中

【讨论】:

  • 这应该是公认的答案,很多人使用 DbContext 删除了基类实现,但从 IdentityDbContext 继承时这一点至关重要
  • 我遇到了同样的错误,在使用接受的答案之前,我阅读了您的答案并意识到我已经删除了那行代码。这解决了问题,并且我确信这是大多数时候有人遇到这个问题的答案。
【解决方案2】:

以下方法OnModelCreating 将创建一个工作上下文。不确定它是否是您想要的映射。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Content>()
        .HasMany(c => c.Editors)
        .WithOptional()
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Content>()
        .HasRequired(c => c.Owner)
        .WithOptional()
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}

【讨论】:

  • 我的错误,我有两个上下文,应用程序上下文和内容上下文,并且我在应用程序上下文中有 IdentityUser 的键映射。现在它就像一个魅力。谢谢。
  • 这是一个错误吗?我有另一个上下文,与项目创建的 ASP.NET 标识上下文无关。那为什么我有最后 3 行?
  • 生成模型实体时出错:Se han detectado uno o varios errores de validación durante la generación del modelo: Usuario_ObPais_Source: : La multiplicidad no es válida en el rol (Role) 'Usuario_ObPais_Source' de la relación' Usuario_ObPais'。 Como las propiedades de Dependent Role no son las propiedades de clave, el límite Superior de la multiplicidad de Dependent Role debe ser '*'.
  • 这工作正常,直到你进入细节。这种方法的问题是,如果您想为同一个用户(facebook 和 google)添加多个用户登录,这将不起作用,因为主键设置为 IdentityUserLogin 中的 UserId。您不能使用相同的主键添加多个外部登录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 2017-10-12
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多