2017-10-23 1 views
0

Mit dem ASP.NET MVC 5-Standardprojekt kann ich unter Verwendung des folgenden Codes säen und Rollen. Ich bemühe mich, die Syntax korrekt zu aktualisieren, nachdem ich den Primärschlüssel für Benutzer angepasst habe.Korrekte Syntax für die Seeding-Rolle/Benutzer nach dem Ändern des Primärschlüssels für Benutzer

ich 2 Fehler in der Konfigurationsklasse

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>'

Wie kann ich diesen Code korrigieren Änderung die CustomUserStor und CustomRoleStor zu machen?

Die Primärschlüssel Änderungen werden auf dieser Basis 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); 
      } 
     } 
    } 
} 

es

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

Antwort

0

Typisch, verwalten mit der folgenden Anpassung funktioniert es bald zu beheben nach der Einlieferung.

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