2016-06-01 100 views
0

我试图在我的ASP Core RC2站点中使用openiddict和EF 7。当我尝试创建使用dotnet ef migrations add <name>我收到以下错误迁移:ASP Core违反了字符串类型参数'TRole'的约束条件

System.ArgumentException: GenericArguments[1], 'System.String', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`3[TUser,TRole,TContext]' violates the constraint of type 'TRole'. ---> System.TypeLoadException: GenericArguments[1], 'System.String', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type parameter 'TRole'. 
at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type) 
at System.RuntimeTypeHandle.Instantiate(Type[] inst) 

在我Startup.cs我有以下几点:

 services.AddIdentity<MyUser, string>() 
      .AddEntityFrameworkStores<MyContext>() 
      .AddDefaultTokenProviders() 
      .AddOpenIddict(); 

我的用户和上下文的定义如下:

  public class MyUser : IdentityUser 
     { 
     } 
     public class MyContext : OpenIddictContext<MyUser> 
     { 
     } 

我认为这个错误与我说对TRole使用字符串string的事实有关。这是不允许的? TRole支持哪些类型?

回答

0

I think the error has something to do with the fact that I said to use string string for TRole. Is this not allowed? What types are supported for TRole?

不,不是。当为ASP.NET Core Identity使用默认实体框架存储时,您的角色实体必须从IdentityRole继承。

0

按照source code

public static IdentityBuilder AddIdentity<TUser, TRole>(this IServiceCollection services, Action<IdentityOptions> setupAction) where TUser : class where TRole : class 

TRole必须参考类型,以便代替在Microsoft.AspNet.Identity.EntityFramework命名空间string使用IdentityRole

services.AddIdentity<MyUser, IdentityRole>() 
     .AddEntityFrameworkStores<MyContext>() 
     .AddDefaultTokenProviders() 
     .AddOpenIddict();