1

来自这个例子
我有一个数据上下文

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'”。
在服务器资源管理器中查看我的数据库,我发现未创建电子邮件列。

4

1 回答 1

1
public class AggregateContext : DbContext
{
    public AggregateContext()
        : base("DefaultConnection")
    {
    }
    public DbSet<BlogEntry> BlogEntries { get; set; }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

忘记定义连接。走我!

于 2012-12-21T02:08:51.527 回答