2013-02-10 62 views
0

我已經明確地設置AccountsContext使用mytestdb作爲SimpleMembership連接字符串,如下 -實體框架:爲什麼SimpleMembership尋找默認連接

public class AccountsContext : DbContext 
{ 
    public AccountsContext() 
     : base("mytestdb") 
    { 
    } 
} 

但在下面的代碼行初始化數據庫仍在尋找DefaultConnection因爲某些原因。儘管我從web.config中完全刪除了對DefaultConnection的引用。

public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute 
    { 
     private static SimpleMembershipInitializer _initializer; 
     private static object _initializerLock = new object(); 
     private static bool _isInitialized; 

     public override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 
      // Ensure ASP.NET Simple Membership is initialized only once per app start 
      LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); 
     } 
    } 

enter image description here

這裏是註釋的部分Web.config文件 -

<configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <!--<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />--> 
    </configSections> 
    <connectionStrings> 
    <add name="mytestdb" connectionString="Data Source=(local);Initial Catalog=mytestdb;User ID=****;Password=****" providerName="System.Data.SqlClient" /> 
    <!--<add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-LakshyaMvc-20130205074346;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-***.mdf" 
     providerName="System.Data.SqlClient" />--> 
    </connectionStrings> 

    <!--<entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    </entityFramework>--> 

在這方面的任何幫助將非常有幫助。

回答

4

~/Filters/InitializeSimpleMembershipAttribute.cs文件中的SimpleMembershipInitializer私有類請看:

private class SimpleMembershipInitializer 
{ 
    public SimpleMembershipInitializer() 
    { 
     Database.SetInitializer<UsersContext>(null); 

     try 
     { 
      using (var context = new UsersContext()) 
      { 
       if (!context.Database.Exists()) 
       { 
        // Create the SimpleMembership database without Entity Framework migration schema 
        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); 
       } 
      } 

      WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); 
     } 
     catch (Exception ex) 
     { 
      throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); 
     } 
    } 
} 

你注意到以下行:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); 

這就是它的來源。順便說一下,如果你看過你的異常堆棧跟蹤,它會指示你到這個位置。

另外,在~/Models/AccountModel.cs文件,你可能有默認定義如下方面:

public class UsersContext : DbContext 
{ 
    public UsersContext() 
     : base("DefaultConnection") 
    { 
    } 

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

超,它就在那裏。謝謝!然而,我找不到UsersContext,我只能看到AccountsContext,其連接字符串我已經修改。 – Abhijeet 2013-02-10 12:58:24