0

Я пытаюсь создать базу данных из первой модели кода. Когда я бегу dotnet ef migrations add или отладки приложения я получаю сообщение об ошибке:аутентификация ядра asp.net с IdentityDbContext <AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>

TypeLoadException: GenericArguments[0], 'OpPISWeb.Models.AppUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' violates the constraint of type parameter 'TUser'. 
System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, int numGenericArgs, ObjectHandleOnStack type) 

в коде:

.AddEntityFrameworkStores<AppWebContext, int>() 

Мой код в ConfigureServices является:

 var res = services 
      .AddIdentity<AppUser, AppRole>(config => 
      { 
       config.User.RequireUniqueEmail = true; 
       config.Password.RequireNonAlphanumeric = false; 
       config.Cookies.ApplicationCookie.AutomaticChallenge = false; 
      }) 
     .AddEntityFrameworkStores<AppWebContext, int>() 
     .AddDefaultTokenProviders(); 

и мои модели EF являются:

[Table("Roles")] 
public partial class AppRole : IdentityRole<int, AppUserRole, AppRoleClaim> 
{ 
} 
[Table("RoleClaims")] 
public partial class AppRoleClaim : IdentityRoleClaim<int> 
{ 
} 
[Table("Users")] 
public partial class AppUser : IdentityUser<int, AppUserClaim, AppUserRole, AppUserLogin> 
{ 
} 

//same for UserClaim, UserLogin, UserRole, UserToken 

и мой DBC-текст:

public partial class AppWebContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken> 
{ 

    public AppWebContext() : base() { 
    } 

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     optionsBuilder.UseSqlServer(@"Server=..."); 
    } 

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

     modelBuilder.Entity<AppUser>(entity => 
     { 
      entity 
      .HasKey(u => u.Id); 
      entity.Property(p => p.Id) 
      .ValueGeneratedOnAdd(); 
     }); 

     modelBuilder.Entity<AppRole>(entity => 
     { 
      entity 
      .HasKey(u => u.Id); 
      entity.Property(p => p.Id) 
      .ValueGeneratedOnAdd(); 
     }); 

     modelBuilder.Entity<AppUserClaim>(entity => 
     { 
      entity 
      .HasKey(u => u.Id); 
      entity.Property(p => p.Id) 
      .ValueGeneratedOnAdd(); 
     }); 

     modelBuilder.Entity<AppUserRole>(entity => 
     { 
      entity 
      .HasKey(u => new { u.RoleId, u.UserId }); 
     }); 

     modelBuilder.Entity<AppRoleClaim>(entity => 
     { 
      entity 
      .HasKey(u => u.Id); 
      entity.Property(p => p.Id) 
      .ValueGeneratedOnAdd(); 
     }); 
    } 
} 

Что я пропустил? Я новичок в ASP.net и ASP.NET Core.
Я использую версию 1.1.

Полная ошибка:

An error occurred while starting the application. 

TypeLoadException: GenericArguments[0], 'OpPISWeb.Models.AppUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' violates the constraint of type parameter 'TUser'. 
System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, int numGenericArgs, ObjectHandleOnStack type) 

ArgumentException: GenericArguments[0], 'OpPISWeb.Models.AppUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'. 
System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) 

TypeLoadException: GenericArguments[0], 'OpPISWeb.Models.AppUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' violates the constraint of type parameter 'TUser'. 
System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, int numGenericArgs, ObjectHandleOnStack type) 
System.RuntimeTypeHandle.Instantiate(Type[] inst) 
System.RuntimeType.MakeGenericType(Type[] instantiation) 

Show raw exception details 
ArgumentException: GenericArguments[0], 'OpPISWeb.Models.AppUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'. 
System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) 
System.RuntimeType.MakeGenericType(Type[] instantiation) 
Microsoft.Extensions.DependencyInjection.IdentityEntityFrameworkBuilderExtensions.GetDefaultServices(Type userType, Type roleType, Type contextType, Type keyType) 
Microsoft.Extensions.DependencyInjection.IdentityEntityFrameworkBuilderExtensions.AddEntityFrameworkStores<TContext, TKey>(IdentityBuilder builder) 
OpPISWeb.Startup.ConfigureServices(IServiceCollection services) 
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) 
Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 

Show raw exception details 
.NET Core X64 v4.1.1.0 | Microsoft.AspNetCore.Hosting version 1.1.0-rtm-22752 | Microsoft Windows 10.0.14393 | Need help? - Yes I do. Please. 

ответ

0

When you are customizing ASP.NET Core Identity, you should not use AddEntityFrameworkStores anymore. Because it will override all of your previous settings and customization to default Identity services.

Цитата Github ответа here.

1

Ваша проблема вызвана .AddEntityFrameworkStores<AppWebContext, int>(). Взгляните на ответы HaoK в конце этого сообщения: https://github.com/aspnet/Identity/issues/1001.

Специально эти 2

"AddEntityFrameworkStores can only be called with a user that derives from IdentityUser. If you are specifying more generic arguments, use IdentityBuilder.AddUserStore() instead."

"AddEntityFrameworkStores can only be called with a role that derives from IdentityRole. If you are specifying more generic arguments, use IdentityBuilder.AddRoleStore() instead."

Так что, если вы хотите, чтобы переопределить UserClaimUserLogin, и все остальные, вам нужно реализовать свой собственный UserStore и RoleStore, а также использовать AddUserStore<T>() и AddRoleStore<T>() вместо AddEntityFrameworkStores.

AppUserStore

public class AppUserStore : UserStore<AppUser, AppRole, AppDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim> 
{ 
    public AppUserStore(AppDbContext dbContext) 
     : base(dbContext) 
    { 
    } 

    protected override AppUserToken CreateUserToken(AppUser user, string loginProvider, string name, string value) 
    { 
     throw new NotImplementedException(); 
    } 

    protected override AppUserLogin CreateUserLogin(AppUser user, UserLoginInfo login) 
    { 
     throw new NotImplementedException(); 
    } 

    protected override AppUserRole CreateUserRole(AppUser user, AppRole role) 
    { 
     throw new NotImplementedException(); 
    } 

    protected override AppUserClaim CreateUserClaim(AppUser user, Claim claim) 
    { 
     throw new NotImplementedException(); 
    } 
} 

AppRoleStore

public class AppRoleStore : RoleStore<AppRole, AppDbContext, int, AppUserRole, AppRoleClaim> 
{ 
    public AppRoleStore(AppDbContext dbContext) 
     : base(dbContext) 
    { 
    } 

    protected override AppRoleClaim CreateRoleClaim(AppRole role, Claim claim) 
    { 
     throw new NotImplementedException(); 
    } 
} 

И в вашем Startup.cs:

services.AddIdentity<AppUser, AppRole>() 
    .AddUserStore<AppUserStore>() 
    .AddRoleStore<AppRoleStore>() 
    // Components that generate the confirm email and reset password tokens 
    .AddDefaultTokenProviders(); 

Теперь я думаю, что вам нужно, чтобы создать свою собственную версию от UserManager, RoleManager и SignInManager. Ниже я расскажу о новых кодах, после того как я все выясню.