2017-08-09 48 views
0

我有一個在VS2015中創建的應用程序(MVC,mvc個人身份驗證)的應用程序,我修改了以下教程將一些自定義字段添加到用戶表中(使用遷移將添加的字段添加到數據庫中)。 然後對於其餘表格(非標識)我使用EntityFramework數據庫 - 首先生成CRUD控制器&意見等 到目前爲止好,一切似乎工作很好。.net身份:錯誤嘗試將字符串(GUID?)鍵更改爲int

現在我試圖修改它,以便Identity表的鍵是整數而不是GUID。我按照這個教程(與MVC更新3), https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity#context

,但之後跟隨本教程中,我構建和運行解決方案,我得到當它試圖生成模型

One or more validation errors were detected during model generation: 
Proyecto.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. 
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined. 

我這個錯誤不知道如何解決它...任何幫助表示讚賞。

我的模型/ IdentityModels.cs:

public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim> 

{ 
    //added custom fields 
    [Column(TypeName = "varchar")] 
    [StringLength(50)] 
    public string Nombre { get; set; } 
    public DateTime FechaAlta { get; set; } 
    public DateTime? FechaUltModif { get; set; } 
    public bool Activo { get; set; } 
    public string Notas { 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 ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 
    } 

    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     //to fix some conflicts using mysql 
     modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).HasMaxLength(255); 
     modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(255); 
     modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255); 
    } 

} 


//aded for int keys 
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) 
    { 
    } 
} 

回答

1

這裏是你的問題:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //your problem is in this line change IdentityRole to CustomRole 
     modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255); 
    } 

使用用繩子主鍵和CustomUserRole定義CustomRole代替IdentityRole因爲IdentityRole你正在使用int

如果你去的IdentityRole定義,你會發現它的擴展IdentityRole<string, IdentityUserRole>和爲什麼您收到錯誤

Currected代碼:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    base.OnModelCreating(modelBuilder); 
    //to fix some conflicts using mysql 
    modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).HasMaxLength(255); 
    modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(255); 
    modelBuilder.Entity<CustomRole>().Property(r => r.Name).HasMaxLength(255); 
} 
+0

大!這是問題所在。謝謝 – patsy2k