2017-10-23 15 views
0

デフォルトのASP.NET MVC 5プロジェクトを使用して、次のコードを使用してシードおよびロールを設定できます。私は、ユーザーの主キーをカスタマイズした後、これを更新するために正しい構文を取得するのに苦労しています。ユーザーのプライマリキーを変更した後に役割/ユーザーをシードするための正しい構文

私はCustomUserStoreとCustomRoleStoreへの変更を考慮するために、このコードを修正するにはどうすればよいConfigurationクラスで2つのエラー

The type 'ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore<TUser>'. There is no implicit reference conversion from 'ApplicationUser' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.

Argument 1: cannot convert from 'Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>' to 'Microsoft.AspNet.Identity.IUserStore<ApplicationUser, int>'

を取得しますか?

主キーの変更は、それは以下のカスタマイズ

namespace IR.Models 
{ 

    public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
    { 
     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 ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
    { 
     public ApplicationDbContext() : base("DefaultConnection") 
     { 
     } 

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

答えて

0

典型的に動作させるために、このChange Primary Key for Users in ASP.NET Identity

namespace IR.Migrations 
{ 
    using System; 
    using System.Data.Entity; 
    using System.Data.Entity.Migrations; 
    using System.Linq; 

    internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext> 
    { 
     public Configuration() 
     { 
      AutomaticMigrationsEnabled = false; 
     } 

     protected override void Seed(ApplicationDbContext context) 
     { 

      if (!context.Roles.Any(r => r.Name == "Administrator")) 
      { 
       var store = new RoleStore<IdentityRole>(context); 
       var manager = new RoleManager<IdentityRole>(store); 
       var role = new IdentityRole { Name = "Administrator" }; 
       manager.Create(role); 
      } 


      SeedNewUser(context, "[email protected]", "****", "Administrator"); 

     } 

     private async void SeedNewUser(ApplicationDbContext context, string email, string dummyPassword, string role) 
     { 
      if (!context.Users.Any(u => u.UserName == email)) 
      { 
       var store = new UserStore<ApplicationUser>(context); 
       var manager = new UserManager<ApplicationUser>(store); 
       var user = new ApplicationUser { UserName = email, Email = email, LockoutEnabled = false, EmailConfirmed = true, SecurityStamp = Guid.NewGuid().ToString() }; 

       manager.Create(user, dummyPassword); 
       manager.AddToRole(user.Id, role); 
      } 
     } 
    } 
} 

に基づいていますが、投稿後すぐにそれを修正するために管理します。

protected override void Seed(ApplicationDbContext context) 
{ 

    if (!context.Roles.Any(r => r.Name == "Administrator")) 
    { 
     var store = new CustomRoleStore(context); 
     var manager = new RoleManager<CustomRole, int>(store); 
     var role = new CustomRole { Name = "Administrator" }; 
     manager.Create(role); 
    } 

    SeedNewUser(context, "[email protected]", "****", "Administrator"); 

} 

private async void SeedNewUser(ApplicationDbContext context, string email, string dummyPassword, string role) 
{ 
    if (!context.Users.Any(u => u.UserName == email)) 
    { 
     var store = new CustomUserStore(context); 
     var manager = new ApplicationUserManager(store); 
     var user = new ApplicationUser { UserName = email, Email = email, LockoutEnabled = false, EmailConfirmed = true, SecurityStamp = Guid.NewGuid().ToString() }; 

     manager.Create(user, dummyPassword); 
     manager.AddToRole(user.Id, role); 
    } 
} 
関連する問題