实体代码优先+ MVC 4网络安全=发送给我

时间:2012-12-21 02:05:19

标签: entity-framework asp.net-mvc-4 ef-code-first security code-first

来自This Example
我有一个数据上下文

public class AggregateContext : DbContext
{
    public DbSet<BlogEntry> BlogEntries { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
}

在app开始我有这个

        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(                       “管理员”,                       “密码”,                       new {Email =“please@help.me”});

使用sqlexception“无效的列名'电子邮件'。”
在服务器资源管理器中查看我的数据库,我看到没有创建电子邮件列。

1 个答案:

答案 0 :(得分:1)

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

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

忘记定义连接。去吧!