【问题标题】:Map tables using fluent api in asp.net MVC5 EF6?在 asp.net MVC5 EF6 中使用流利的 api 映射表?
【发布时间】:2013-10-28 18:33:04
【问题描述】:

我正在尝试将个人资料/会员信息添加到我的 MVC5 应用程序中并添加配置映射。

我收到以下错误消息:

my.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' 没有 键定义。定义此 EntityType 的键。

my.Models.IdentityUserRole: : EntityType 'IdentityUserRole' 没有密钥 定义。定义此 EntityType 的键。

IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' 是 基于没有定义键的类型“IdentityUserLogin”。

IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' 是基于 在没有定义键的类型“IdentityUserRole”上。

public class ApplicationUser : IdentityUser
{
    public string City { get; set; }
    public string Discriminator { get; set; }

    public string Address { get; set; }     
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());           
    }
}

【问题讨论】:

    标签: claims-based-identity entity-framework-6 asp.net-identity


    【解决方案1】:

    致电base.OnModelCreating(modelBuilder) 并没有为我解决问题。

    Microsoft.AspNet.Identity.EntityFramework 的行为在 VS2013-Preview、VS2013-RC 和 VS2013-RTM 中似乎有所不同。我正在使用 RTM 版本。

    从 IdentityUser 继承后,我必须重新创建模型中的所有其他主键才能使其工作:

    public class ApplicationUser : IdentityUser
    {
        public string DisplayName { get; set; }
    }
    
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext() : base("DefaultConnection") { }
    
        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 });
        }
    

    (见Configuring/Mapping Properties and Types with the Fluent API

    我猜 AspNet.Identity.EntityFramework 的工作正在进行中,这将得到修复 (?)

    【讨论】:

    • 我不得不做同样的事情来解决这个问题。我以为 EF 会自动挑选实体 ID?
    • 这一切都为我解决了!非常感谢 - 应标记为已回答:)
    • 我遇到了类似的问题并尝试了这个解决方案并认为它解决了问题,但是我在数据库中获得了一些额外的表,例如 IdentityUsers,而且我还有 AspNetUsers 表。我做错了什么?
    • @LaurenceNyein place base.OnModelCreating(modelBuilder) 作为方法中的最后一次调用。
    【解决方案2】:

    添加配置后调用base.OnModelCreating(modelBuilder)

    【讨论】:

    • 如果这不起作用..尝试在配置之前调用它
    • 如果使用 IdentityDbContext&lt;ApplicationUser&gt;OnModelCreating 被覆盖,并且我没有手动配置 IdentityUserLoginIdentityUserRoleIdentityUserClaim 属性或流畅的 API。
    【解决方案3】:

    围绕OnModelCreating方法执行以下步骤,并在每个步骤后进行测试以确保生效:

    1. 确保您有一个Context,以防止他们的规则冲突。
    2. 拨打base.OnModelCreating(modelBuilder);里面提到 方法(首先)
    3. 在方法中添加以下加预览步骤:


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

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2013-10-27
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2011-05-10
      • 1970-01-01
      相关资源
      最近更新 更多