我正在使用Identity 3.我想在啓動時種下管理員用戶。使用int id的用戶在Asp.net核心中失敗MVC6
我使用github上的例子SwiftCourier將字符串id轉換爲「int」id。身份3在某些方面有所不同,這適用於SwiftCourier示例中顯示的ID爲「int」的設置,與Identity 2和「int」ID設置相比較。
public class User : IdentityUser<int>{...}
我的角色之後:
public class Role : IdentityRole<int>
和startup.cs在配置服務我進入了這個:
我已經把 「INT」 我的用戶之後完成這個
services.AddIdentity<User, Role>(options => options.User.AllowedUserNameCharacters = null)
.AddEntityFrameworkStores<JobsDbContext, int>()
.AddUserStore<UserStore<User, Role, JobsDbContext, int>>()
.AddRoleStore<RoleStore<Role, JobsDbContext, int>>()
.AddDefaultTokenProviders();
然後我用它來播種角色,它與整數ID一起工作:
public static void EnsureRolesCreated(this IApplicationBuilder app)
{
var context = app.ApplicationServices.GetService<JobsDbContext>();
if (context.AllMigrationsApplied())
{
var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>();
var Roles = new List<Role>();
Roles.Add(new Role { Name = "Admin", Description = "Users able to access all areas" });
Roles.Add(new Role { Name = "Technician", Description = "Users able to access just the schedule" });
foreach (var role in Roles)
{
if (!roleManager.RoleExistsAsync(role.Name.ToUpper()).Result)
{
roleManager.CreateAsync(role);
}
}
}
}
到目前爲止好...我要種子只有一個管理員用戶下手,這是我失敗...
我已經使用了以下#1 example通過@Guy。
我掙扎UserStore這一行:
await new UserStore<User>(context).CreateAsync(userWithRoles.User);
它失敗的「用戶」,這是我的ApplicationUser只是改名。
我得到的錯誤是:
The type 'JobsLedger.Models.Identity.User' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore<TUser>'. There is no implicit reference conversion from 'JobsLedger.Models.Identity.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser<string>'.
這似乎是一個事實,即該ID是「int」我認爲
如何讓UserStore工作與「INT」抱怨在這方面??它被設置爲Startup.cs中的int,如上所示。
如果我必須創建一個自定義的「UserStore」,它與「int」一起工作..您如何在Identity 3中執行此操作?
我舉了一個將用戶添加到角色的例子,它有幫助嗎?如果確實如此,請將答案標記爲正確答案 – jamiedanq
我剛剛完成了這項工作。我不能讓用戶在這裏使用int ID創建,更不用說爲用戶添加角色..它在用戶上的失敗.. – si2030