user1238784:
私は、次の移行の設定クラスがあります。
namespace MVC_Authentication.Migrations
{
using Microsoft.AspNet.Identity.EntityFramework;
using MVC_Authentication.Models;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Threading.Tasks;
internal sealed class MigrationConfiguration : DbMigrationsConfiguration<ApplicationDbContext>
{
public MigrationConfiguration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "MVC_Authentication.Models.ApplicationDbContext";
}
protected override void Seed(ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
SeedDatabase(context).GetAwaiter().GetResult();
}
protected async Task SeedDatabase(ApplicationDbContext ctx)
{
var roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(ctx));
if (await roleManager.FindByNameAsync("Administrator") == null)
await roleManager.CreateAsync(new IdentityRole("Administrator"));
if (await roleManager.FindByNameAsync("User") == null)
await roleManager.CreateAsync(new IdentityRole("User"));
ApplicationUserManager userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(ctx));
var user_Admin = new ApplicationUser()
{
SecurityStamp = Guid.NewGuid().ToString(),
UserName = "Admin",
Email = "myEmail",
EmailConfirmed = true
};
if (await userManager.FindByNameAsync(user_Admin.UserName) == null)
{
await userManager.CreateAsync(user_Admin, "MyPassword");
await userManager.AddToRoleAsync(user_Admin.Id, "User");
await userManager.AddToRoleAsync(user_Admin.Id, "Administrator");
}
}
}
}
しかし、実行が到着したとき:
if (await roleManager.FindByNameAsync("Administrator") == null)
アプリケーションがロックし、私は待って待機することができます。たぶん私はここにRoleManagerとのUserManagerを使用するべきではないのですか?これは、シードロールとユーザーへの唯一の方法です。可能なもののいずれかのヒントのおかげで間違って行くことができます。
Jerdineワイズ:
RoleManagerではなく、ApplicationRoleManagerで同期方法を試してみてください。
var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.FindName(...);