每当我运行我的应用程序时,都会收到此错误:
'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`8 [TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken]'违反了类型参数'TUser'的约束。
这是我的代码段:
public class ApplicationUser : IdentityUser<string,ApplicationUserClaim,ApplicationUserRole,ApplicationUserLogin>
public class ApplicationRole : IdentityRole<string, ApplicationUserRole,IdentityRoleClaim<string>>
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole,string,ApplicationUserClaim,ApplicationUserRole,ApplicationUserLogin,IdentityRoleClaim<string>,IdentityUserToken<string>>
这是我的startup.cs片段,它会引发错误。
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
我尝试过添加密钥类型(如上所述here),但没有帮助。
如果您想在本地测试,请Link到Full Project。
请帮助。谢谢。
编辑:
1: public class ApplicationUserLogin: IdentityUserLogin < string > {}
2: public class ApplicationUserClaim: IdentityUserClaim < string > {}
3: public class ApplicationUserRole: IdentityUserRole < string > {}
4:
5: // Must be expressed in terms of our custom Role and other types:
6: public class ApplicationUser
7: : IdentityUser < string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin > {
8: public ApplicationUser() {
9: this.Id = Guid.NewGuid().ToString();
10:
11: // Add any custom User properties/code here
12: }
13:
14:
15: public async Task < ClaimsIdentity >
16: GenerateUserIdentityAsync(UserManager < ApplicationUser > manager) {
17: var authtype = AuthenticationTypes.Basic;
18:
19: var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authtype);
20: return userIdentity;
21: }
22: }
23:
24:
25: // Must be expressed in terms of our custom UserRole:
26: public class ApplicationRole:
27: IdentityRole < string, ApplicationUserRole, IdentityRoleClaim < string >> {
28: public ApplicationRole() {
29: this.Id = Guid.NewGuid().ToString();
30: }
31:
32: public ApplicationRole(string name): this() {
33: this.Name = name;
34: }
35:
36: // Add any custom Role properties/code here
37: }
38:
39:
40: // Must be expressed in terms of our custom types:
41: public class ApplicationDbContext
42: : IdentityDbContext < ApplicationUser, ApplicationRole,
43: string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, IdentityRoleClaim < string > , IdentityUserToken < string >> {
44:
45: public ApplicationDbContext(DbContextOptions options): base(options) {
46: Database.EnsureCreated();
47: }
48:
49: static ApplicationDbContext() {
50:
51: }
52:
53: //public static ApplicationDbContext Create()
54: //{
55: // return new ApplicationDbContext();
56: //}
57:
58: // Add the ApplicationGroups property:
59: public virtual DbSet < ApplicationGroup > ApplicationGroups {
60: get;
61: set;
62: }
63:
64: // Override OnModelsCreating:
65: protected override void OnModelCreating(ModelBuilder modelBuilder) {
66: base.OnModelCreating(modelBuilder);
67:
68: modelBuilder.Entity < ApplicationGroup > ()
69: .HasMany < ApplicationUserGroup > ((ApplicationGroup g) => g.ApplicationUsers)
70: .WithOne()
71: .HasForeignKey((ApplicationUserGroup g) => g.ApplicationUserId);
72: modelBuilder.Entity < ApplicationUserGroup > ()
73: .HasKey((ApplicationUserGroup r) =>
74: new {
75: ApplicationUserId = r.ApplicationUserId,
76: ApplicationGroupId = r.ApplicationGroupId
77: });
78:
79: modelBuilder.Entity < ApplicationGroup > ()
80: .HasMany < ApplicationGroupRole > ((ApplicationGroup g) => g.ApplicationRoles)
81: .WithOne().HasForeignKey((ApplicationGroupRole ap) => ap.ApplicationGroupId);
82: modelBuilder.Entity < ApplicationGroupRole > ().HasKey((ApplicationGroupRole gr) =>
83: new {
84: ApplicationRoleId = gr.ApplicationRoleId,
85: ApplicationGroupId = gr.ApplicationGroupId
86: });
87:
88: }
89: }
90:
91:
92: // Most likely won't need to customize these either, but they were needed because we implemented
93: // custom versions of all the other types:
94: public class ApplicationUserStore
95: : UserStore < ApplicationUser, ApplicationRole, DbContext, string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, IdentityUserToken < string >> ,
96: IUserStore < ApplicationUser > ,
97: IDisposable {
98: public ApplicationUserStore(DbContextOptions options): this(new IdentityDbContext(options)) {
99: base.Dispose();
100: }
101:
102: public ApplicationUserStore(IdentityDbContext context): base(context) {}
103:
104: protected override ApplicationUserClaim CreateUserClaim(ApplicationUser user, Claim claim) {
105: throw new NotImplementedException();
106: }
107:
108: protected override ApplicationUserLogin CreateUserLogin(ApplicationUser user, UserLoginInfo login) {
109: throw new NotImplementedException();
110: }
111:
112: protected override ApplicationUserRole CreateUserRole(ApplicationUser user, ApplicationRole role) {
113: throw new NotImplementedException();
114: }
115:
116: protected override IdentityUserToken < string > CreateUserToken(ApplicationUser user, string loginProvider, string name, string value) {
117: throw new NotImplementedException();
118: }
119: }
120:
121:
122: public class ApplicationRoleStore
123: : RoleStore < ApplicationRole, DbContext, string, ApplicationUserRole, IdentityRoleClaim < string >> ,
124: IQueryableRoleStore < ApplicationRole > ,
125: IRoleStore < ApplicationRole > , IDisposable {
126: public ApplicationRoleStore(DbContextOptions options): base(new IdentityDbContext(options)) {
127: base.Dispose();
128: }
129:
130: public ApplicationRoleStore(ApplicationDbContext context): base(context) {}
131:
132: protected override IdentityRoleClaim < string > CreateRoleClaim(ApplicationRole role, Claim claim) {
133: throw new NotImplementedException();
134: }
135: }
136:
137:
138: public class ApplicationGroup {
139: public ApplicationGroup() {
140: this.Id = Guid.NewGuid().ToString();
141: this.ApplicationRoles = new List < ApplicationGroupRole > ();
142: this.ApplicationUsers = new List < ApplicationUserGroup > ();
143: }
144:
145: public ApplicationGroup(string name): this() {
146: this.Name = name;
147: }
148:
149: public ApplicationGroup(string name, string description): this(name) {
150: this.Description = description;
151: }
152:
153: [Key]
154: public string Id {
155: get;
156: set;
157: }
158: public string Name {
159: get;
160: set;
161: }
162: public string Description {
163: get;
164: set;
165: }
166: public virtual ICollection < ApplicationGroupRole > ApplicationRoles {
167: get;
168: set;
169: }
170: public virtual ICollection < ApplicationUserGroup > ApplicationUsers {
171: get;
172: set;
173: }
174: }
175:
176:
177: public class ApplicationUserGroup {
178: public string ApplicationUserId {
179: get;
180: set;
181: }
182: public string ApplicationGroupId {
183: get;
184: set;
185: }
186: }
187:
188: public class ApplicationGroupRole {
189: public string ApplicationGroupId {
190: get;
191: set;
192: }
193: public string ApplicationRoleId {
194: get;
195: set;
196: }
197: }