Skip to content
This repository was archived by the owner on Dec 20, 2018. It is now read-only.
This repository was archived by the owner on Dec 20, 2018. It is now read-only.

Make AddEntityFrameworkStores smarter with generics #1001

@lixiaoyuan

Description

@lixiaoyuan
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: GenericArguments[0], 'BusinessDb.Cor.EntityModel.SystemUser`1[System.Guid]', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'. ---> System.TypeLoadException: GenericArguments[0], 'BusinessDb.Cor.EntityModel.SystemUser`1[System.Guid]', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`8[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken]' violates the constraint of type parameter 'TUser'.
   at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.Instantiate(Type[] inst)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at Microsoft.Extensions.DependencyInjection.IdentityEntityFrameworkBuilderExtensions.GetDefaultServices(Type userType, Type roleType, Type contextType, Type keyType)
   at Microsoft.Extensions.DependencyInjection.IdentityEntityFrameworkBuilderExtensions.AddEntityFrameworkStores[TContext,TKey](IdentityBuilder builder)
   at ErpApplication.Web.Startup.ConfigureServices(IServiceCollection services) in D:\外接系统\ErpWebApplication\src\ErpApplication.Web\Startup.cs:line 40
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection exportServices)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Here is my code

ApplicationDbContext.cs

public class ApplicationDbContext : IdentityDbContext<
        SystemUser<Guid>,
        EntityModel.IdentityRole, Guid,IdentityUserClaim,IdentityUserRole,IdentityUserLogin,IdentityRoleClaim,IdentityUserToken>
    {
        public ApplicationDbContext(DbContextOptions options)
            : base(options)
        {
        }        
        protected ApplicationDbContext()
        {            
        }
   }

Identity.cs

public class SystemUser<TKey> : IdentityUser<TKey,IdentityUserClaim,IdentityUserRole,IdentityUserLogin>
        where TKey : IEquatable<TKey>
    {
    }
    public class IdentityUserClaim : IdentityUserClaim<Guid>
    {
    }
    public class IdentityUserLogin : IdentityUserLogin<Guid>
    {
    }
    public class IdentityUserRole : IdentityUserRole<Guid>
    {
    }
    public class IdentityUserToken : IdentityUserToken<Guid>
    {
    }
    public class IdentityRole : IdentityRole<Guid,IdentityUserRole,IdentityRoleClaim>
    {
    }
    public class IdentityRoleClaim : IdentityRoleClaim<Guid>
    {
    }

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>((builder =>
                {
                    builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                }));
//this  throw new Exception
            services.AddIdentity<SystemUser<Guid>, BusinessDb.Cor.EntityModel.IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext,Guid>()
                .AddDefaultTokenProviders();

            services.AddMvc();
        }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions