ASP.NET MVC 4 - WebSecurity.InitializeDatabaseConnection多次调用错误

时间:2013-06-10 08:32:18

标签: asp.net-mvc asp.net-mvc-4 entity-framework-5 ef-migrations simplemembership

我的Migrations / Configuration.cs中有一个Seed方法:

  protected override void Seed(MVC4App.Models.MVC4Entities context)
  {
     try
     {
        // Initialize database for Account
        WebSecurity.InitializeDatabaseConnection(
           "MVC4Entities", "Users", "UserId", "Email", autoCreateTables: true);

如果我尝试在我的开发机器上运行'update-database -verbose',我会收到此错误:

The "WebSecurity.InitializeDatabaseConnection" method can be called only once.

这是我的代码中唯一被调用的地方。我该如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:2)

添加初始化检查:

if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection(
           "MVC4Entities", "Users", "UserId", "Email", autoCreateTables: true);
}