2016-06-01 99 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();