Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 迁移配置类中的My seeder锁定应用程序(Asp.net MVC)_Asp.net Mvc_Seeding - Fatal编程技术网

Asp.net mvc 迁移配置类中的My seeder锁定应用程序(Asp.net MVC)

Asp.net mvc 迁移配置类中的My seeder锁定应用程序(Asp.net MVC),asp.net-mvc,seeding,Asp.net Mvc,Seeding,我有以下迁移配置类: 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.L

我有以下迁移配置类:

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");
        }
    }
}
应用程序被锁定,我可以等待再等待。 也许我不应该在这里使用RoleManager和UserManager?这是播种角色和用户的唯一方法。
感谢您提供任何可能出错的提示。

在RoleManager中尝试同步方法,而不是在ApplicationRoleManager中

var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.FindName(...);
var-roleStore=新的roleStore(db);
var roleManager=新roleManager(roleStore);
roleManager.FindName(…);

如果同步版本的行为方式相同,您是否会尝试同步版本
roleManager.FindByName()
intellisense没有提供同步版本FindByName(),请尝试roleManager而不是ApplicationRoleManager<代码>var roleStore=新roleStore(db);var roleManager=新roleManager(roleStore)成功了。谢谢。哦,我不知道那会是解决办法,哈哈。我添加了答案,请标记为正确答案,谢谢!
var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.FindName(...);