来自这个例子
我有一个数据上下文
public class AggregateContext : DbContext
{
public DbSet<BlogEntry> BlogEntries { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
}
在应用程序开始我有这个
Database.SetInitializer(new TestingDbInitializer());
new AggregateContext().UserProfiles.Find(1);
我的初始化程序看起来像这样
public class TestingDbInitializer : DropCreateDatabaseAlways<AggregateContext>
{
protected override void Seed(AggregateContext context)
{
AccountsContext(context);
// add a bunch of Lorems to the blog. does call context.SaveChanges();
BlogsContext(context);
}
void AccountsContext(AggregateContext context)
{
WebSecurity.InitializeDatabaseConnection(
"DefaultConnection",
"UserProfile",
"UserId",
"UserName",
autoCreateTables: true);
//create Admin
if (!WebSecurity.ConfirmAccount("Admin"))
{
var confirm = WebSecurity.CreateUserAndAccount(
"Admin",
"password",
new { Email = "please@help.me" });
if (!Roles.RoleExists("Admin"))
Roles.CreateRole("Admin");
Roles.AddUserToRole("Admin", "Admin");
}
}
当我运行它时,我在这条线上崩溃。
var confirm = WebSecurity.CreateUserAndAccount("Admin", "password", new { Email = "please@help.me" });
带有 sqlexception“无效的列名 'Email'”。
在服务器资源管理器中查看我的数据库,我发现未创建电子邮件列。