21

Próbuję dostosować ASP.NET Identity 3 tak, że używa klawiszy integer:Dlaczego to narusza ograniczenie typu?

public class ApplicationUserLogin : IdentityUserLogin<int> { } 
public class ApplicationUserRole : IdentityUserRole<int> { } 
public class ApplicationUserClaim : IdentityUserClaim<int> { } 

public sealed class ApplicationRole : IdentityRole<int> 
{ 
    public ApplicationRole() { } 
    public ApplicationRole(string name) { Name = name; } 
} 

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext, int> 
{ 
    public ApplicationUserStore(ApplicationDbContext context) : base(context) { } 
} 

public class ApplicationRoleStore : RoleStore<ApplicationRole, ApplicationDbContext, int> 
{ 
    public ApplicationRoleStore(ApplicationDbContext context) : base(context) { } 
} 

public class ApplicationUser : IdentityUser<int> 
{ 
} 

public sealed class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int> 
{ 
    private static bool _created; 

    public ApplicationDbContext() 
    { 
    // Create the database and schema if it doesn't exist 
    if (!_created) { 
     Database.AsRelational().Create(); 
     Database.AsRelational().CreateTables(); 
     _created = true; 
    } 
    } 
} 

To kompiluje w porządku, ale następnie generuje błąd wykonania:

System.TypeLoadException

GenericArguments[0], 'TeacherPlanner.Models.ApplicationUser', on 'Microsoft.AspNet.Identity.EntityFramework.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type parameter 'TUser'.

Podpis dla UserStore jest :

public class UserStore<TUser, TRole, TContext, TKey> 
where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser<TKey> 
where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityRole<TKey> 
where TContext : Microsoft.Data.Entity.DbContext 
where TKey : System.IEquatable<TKey> 

ApplicationUser właśnie IdentityUser<int>. Czy tego właśnie nie szuka?

+1

Czy przez przypadek zadeklarowałeś nowy typ w projekcie o tej samej nazwie co IdentityUser? Robię to czasami wybierając "deklaruj typ" zamiast "dodawaj za pomocą instrukcji" w CodeRush. –

+0

Nie, właśnie sprawdziłem! – James

Odpowiedz

45

Wystąpił ten problem. To zawiesza się na pliku startup.cs. zmienił

services.AddIdentity<ApplicationUser, ApplicationIdentityRole>() 
       .AddEntityFrameworkStores<ApplicationDbContext>() 
       .AddDefaultTokenProviders(); 

do

services.AddIdentity<ApplicationUser, ApplicationIdentityRole>() 
       .AddEntityFrameworkStores<ApplicationDbContext,int>() 
       .AddDefaultTokenProviders(); 

deklarując kluczem typu wydawało się ominąć katastrofie

+0

Obawiałem się, że będę musiał zastąpić wszystkie klasy i metody Tożsamości, ponieważ mam długie klucze, ale wszystko działa z tą małą poprawką. Dzięki! – Atchitutchuk

9

wpadł tego problemu, jak również. Musiałem dodać również typ klucza IdentityRole, ponieważ wciąż wyrzucał ten sam błąd.

 services.AddIdentity<ApplicationUser, IdentityRole<int>>() 
      .AddEntityFrameworkStores<ApplicationDbContext,int>() 
      .AddDefaultTokenProviders(); 
0

Mam teraz ten sam problem. Poprawka była inna. Dlatego zamieszczam go tutaj. Może pomóc innym.

services.AddIdentity<ApplicationUser, ApplicationRole>() 
     .AddEntityFrameworkStores<ApplicationDbContext,int>() 
     .AddDefaultTokenProviders(); 

musiałem utworzyć pustą klasę dla ApplicationRole dziedziczenie IdentityRole<int>.

public class ApplicationRole : IdentityRole<int> 
{ 

} 
Powiązane problemy