asp.net核心mvc6种子角色在重复创建数据库时会产生不同的结果

时间:2016-04-30 12:06:22

标签: asp.net-core asp.net-core-mvc asp.net-identity-3

使用带有整数ID 的Identity 3.0种子,我得到了奇怪的结果。我正在播种角色 - 只有两个 - 使用它:

using JobsLedger.DAL;
using JobsLedger.Models.Identity;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;

namespace JobsLedger.Models.Seeding
{
    public static class SeedRoles
    {
    public static void EnsureRolesCreated(this IApplicationBuilder app)
    {
        var _context = app.ApplicationServices.GetService<JobsDbContext>();

        if (_context.AllMigrationsApplied())
        {
            if (!_context.Roles.Any())
            {
                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).Result)
                    {
                        roleManager.CreateAsync(role);
                    }
                }
                _context.SaveChanges();
            }
        }
    }
}

}

我从github上的Unicorn example得到的。

使用名为Description:

的额外字段,角色如下所示
public class Role : IdentityRole<int>
{
    public Role() : base() { }
    public Role(string name) { Name = name; }
    public string Description { get; set; }
}

在我的启动中,我在configure()方法中有这个:

app.EnsureRolesCreated();

我已将项目配置为使用以下命令通过Startup.cs重新创建数据库:

serviceScope.ServiceProvider.GetService<JobsDbContext>().Database.Migrate();

数据库在启动时创建,如果它不在那里......很好 - 它可以工作。

现在,当我检查角色时,我要么只获得第二个角色,要么两个角色两次.. 4个记录..或者,很少只是两个记录,这是它的假设。

我尝试检查方法中的角色,并在添加角色之前检查是否有任何角色。

我删除了表的内容,所以所有行再次运行它,我只得到第二个角色,ID为&#34; 2&#34; ...好像它的id是2但是为什么只是一个记录?

删除了数据库并重新运行了项目,现在它的第二个角色的ID为&#34; 1&#34; ...但仍只是一条记录。

重写方法更接近Unicorn example,认为这可能是我编写方法的方式..

public static void EnsureRolesCreated(this IApplicationBuilder app)
    {
        var _context = app.ApplicationServices.GetService<JobsDbContext>();

        if (_context.AllMigrationsApplied())
        {
            if (!_context.Roles.Any())
            {
                var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>();

                var Roles = new string[,]
                {
                    {"Admin","Users able to access all areas" },
                    {"Technician","Users able to access just the schedule" }
                };

                for (int i = 0; i < Roles.GetLength(0); i++)
                {
                    if (!roleManager.RoleExistsAsync(Roles[i, 0].ToUpper()).Result)
                    {
                        roleManager.CreateAsync(new Role { Name = Roles[i, 0], Description = Roles[i, 1] });
                    }
                }
            }
        }
    }

第一次运行它时效果很好..删除了数据库并再次运行它。仅保存了第二条记录..然后我再次删除了数据库并第三次运行并获得了4条记录。那两个角色都是两次?

我做错了吗?代码似乎可以创建两个角色,但也许我错过了什么?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,所以我相信我已经解决了这个问题,最后这很简单。我将静态方法转换为异步静态方法,现在它似乎每次都完美地运行:

public static async void EnsureRolesCreatedAsync(this IApplicationBuilder app)
    {
        var _context = app.ApplicationServices.GetService<JobsDbContext>();

        if (_context.AllMigrationsApplied())
        {
            if (!_context.Roles.Any())
            {
                var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>();

                var Roles = new string[,]
                {
                    {"Admin","Users able to access all areas" },
                    {"Technician","Users able to access just the schedule" }
                };

                for (int i = 0; i < Roles.GetLength(0); i++)
                {
                    if (!roleManager.RoleExistsAsync(Roles[i, 0].ToUpper()).Result)
                    {
                        Debug.Write("add role");
                        await roleManager.CreateAsync(new Role { Name = Roles[i, 0], Description = Roles[i, 1] });
                    }
                }
            }
相关问题