【发布时间】:2015-03-05 15:42:30
【问题描述】:
我正在开发一个新的 MVC5 应用程序,该应用程序专门用于测试这一点。
我有 两个 数据库上下文。 ApplicationDbContext 对应的configuration.cs 如下:
using FB.DOMAIN.Authentication;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity.Migrations;
namespace FB.DAL.Migrations.ApplicationDbContext
{
internal sealed class Configuration : DbMigrationsConfiguration<DataContexts.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
MigrationsDirectory = @"Migrations\ApplicationDbContext";
}
protected override void Seed(DataContexts.ApplicationDbContext context)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
if (!roleManager.RoleExists("Admin"))
{
roleManager.Create(new IdentityRole("Admin"));
}
var user = new ApplicationUser { UserName = "james@world.com" };
if (userManager.FindByName("james@world.com") != null) return;
var result = userManager.Create(user, "Password123!");
if (result.Succeeded)
{
userManager.AddToRole(user.Id, "Admin");
}
}
}
}
上面的代码不应该用用户为我的数据库播种吗?可惜没有!当我查询AspNetUsers 表时,它是空白的!
我做错了什么? :(
【问题讨论】:
-
我在应用程序中移动了我的身份种子代码,因为如果您的上下文派生自 IdentityDbContext,则 OWIN 在种子方法中不可用。见stackoverflow.com/questions/23574591/…
标签: entity-framework-6 entity-framework-migrations asp.net-mvc-5.2