播种数据库

时间:2016-05-23 10:12:21

标签: asp.net-core asp.net-identity entity-framework-core

当我运行我的应用程序时,会弹出ArgumentNullException,并显示消息" Value cannot be null."在if方法的第一个EnsureSeedDataAsync条件中。当它到达此点时,_roleManager不为空。赋予构造函数的参数也不为空。

FletnixUser是一个继承自IdentityUser的空类,FletnixRole是一个继承自IdentityRole btw的空类。

有谁知道可能导致此错误的原因是什么?我无法找到原因,我甚至重建了我的数据库并将其迁移到使用身份框架。我尝试在FletnixSeedData构造函数中注释掉所有角色函数和第三个参数,以便它只创建一个可以工作的用户。

我已经包含了startup.cs类,因为该类是调用FletnixSeedData类的唯一类。

FletnixSeedData.cs

public class FletnixSeedData {
    private FletnixContext _context;
    private UserManager<FletnixUser> _userManager;
    private RoleManager<FletnixRole> _roleManager;

    public FletnixSeedData(FletnixContext context, UserManager<FletnixUser> userManager, RoleManager<FletnixRole> roleManager) {
        _context = context;
        _userManager = userManager;
        _roleManager = roleManager;
    }

    public async Task EnsureSeedDataAsync() {
        // If roles and users exist, delete
        if (!await _roleManager.RoleExistsAsync("Customer")) {
            await _roleManager.DeleteAsync(await _roleManager.FindByNameAsync("Customer"));
        }
        if (await _roleManager.FindByNameAsync("Admin") != null) {
            await _roleManager.DeleteAsync(await _roleManager.FindByNameAsync("Admin"));
        }
        if (await _roleManager.FindByNameAsync("FinancialManager") != null) {
            await _roleManager.DeleteAsync(await _roleManager.FindByNameAsync("FinancialManager"));
        }
        if (await _roleManager.FindByNameAsync("CEO") != null) {
            await _roleManager.DeleteAsync(await _roleManager.FindByNameAsync("CEO"));
        }

        if (await _userManager.FindByEmailAsync("myemail@gmail.com") != null) {
            await _userManager.DeleteAsync(await _userManager.FindByEmailAsync("myemail@gmail.com"));
        }


        // Recreate roles and users
        var customerRole = new FletnixRole {
            Name = "Customer"
        };
        var adminRole = new FletnixRole {
            Name = "Admin"
        };
        var financialManagerRole = new FletnixRole {
            Name = "FinancialManager"
        };
        var ceoRole = new FletnixRole {
            Name = "CEO"
        };
        await _roleManager.CreateAsync(customerRole);
        await _roleManager.CreateAsync(adminRole);
        await _roleManager.CreateAsync(financialManagerRole);
        await _roleManager.CreateAsync(ceoRole);

        var newUser = new FletnixUser {
            UserName = "myusername",
            Email = "myemail@gmail.com"
        };
        await _userManager.CreateAsync(newUser, "P@ssw0rd!");
        await _userManager.AddToRoleAsync(newUser, "Customer");
    }
}

Startup.cs - ConfigureServices

    public void ConfigureServices(IServiceCollection services) {
        services.AddMvc();

        services.AddIdentity<FletnixUser, FletnixRole>(config => {
            config.User.RequireUniqueEmail = true;
            config.Password.RequiredLength = 6;
            config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
        }).AddEntityFrameworkStores<FletnixContext>();

        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<FletnixContext>();

        services.AddScoped<IFletnixRepository, FletnixRepository>();
        services.AddTransient<FletnixSeedData>();
    }

Startup.cs - 配置

    public async void Configure(IApplicationBuilder app, FletnixSeedData seeder) {
#if DEBUG
        app.UseDeveloperExceptionPage();
#endif

        app.UseStaticFiles();

        app.UseIdentity();

        app.UseMvc(config => {
            config.MapRoute(
                name: "Default",
                template: "{controller}/{action}/{id?}",
                defaults: new {
                    controller = "Auth", action = "Login"
                }
            );
        });

        await seeder.EnsureSeedDataAsync();
    }

0 个答案:

没有答案