当UserId类型更改为Int时,重命名EF6标识表

时间:2015-03-26 11:32:13

标签: asp.net-mvc entity-framework-6 asp.net-identity

我正在关注该文章,将UserId更改为int http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity#mvcupdate3

一切顺利,我做了那个改变,但我想做更多,我也想重命名表。

为了做到这一点OnModelCreating

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

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

            modelBuilder.Entity<IdentityUser>().ToTable("Accounts").Property(p => p.Id).HasColumnName("UserId");
            modelBuilder.Entity<ApplicationUser>().ToTable("Accounts").Property(p => p.Id).HasColumnName("UserId");
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
            modelBuilder.Entity<IdentityRole>().ToTable("Roles");


        }

然后当我生成迁移(添加迁移)时,我遇到了错误:

Refinery.Web.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Refinery.Web.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

我的实体看起来像这样:

public class ApplicationUser : IdentityUser <int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
    {
        [Key]
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Password { get; set; }

        [ForeignKey("UserId")]
        public virtual List<Option> Options{ get; set; } 

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

    }

    public class CustomUserRole : IdentityUserRole<int> { }
    public class CustomUserClaim : IdentityUserClaim<int> { }
    public class CustomUserLogin : IdentityUserLogin<int> { }

    public class CustomRole : IdentityRole<int, CustomUserRole>
    {
        public CustomRole() { }
        public CustomRole(string name) { Name = name; }
    }

    public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
        CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public CustomUserStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

    public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
    {
        public CustomRoleStore(ApplicationDbContext context)
            : base(context)
        {
        }
    } 

public class Option
    {
        [Key]
        public int Id { get; set; }
        public int UserId { get; set; }
        public string Title { get; set; }
    }
可能有人可以帮忙吗?

  

除非我改变,否则我没有以这种方式重命名身份表这样的问题   UserId类型为int。

1 个答案:

答案 0 :(得分:4)

问题在于,虽然您已实现自定义身份模型,但仍在OnModelCreating方法中引用原始身份模型

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

    modelBuilder.Entity<ApplicationUser>().ToTable("Accounts").Property(p => p.Id).HasColumnName("UserId");
    modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles");
    modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins");
    modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims");
    modelBuilder.Entity<CustomRole>().ToTable("Roles");
}