问题描述:

I have explicitly set AccountsContext to use mytestdb as the connection string for SimpleMembership, as below -

public class AccountsContext : DbContext

{

public AccountsContext()

: base("mytestdb")

{

}

}

But Database initialized in following line of code is still looking for DefaultConnection for some reason. Although I have removed reference to DefaultConnection completely from web.config.

 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);

}

}

Here is the Web.config file with commented sections -

 <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>-->

Any help in this regard will be much helpful.

网友答案:

Take a look at the SimpleMembershipInitializer private class inside the ~/Filters/InitializeSimpleMembershipAttribute.cs file:

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);
        }
    }
}

Do you notice the following line:

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

That's where it is coming from. By the way if you had looked at the exception stacktrace you got it would have pointed you to this location.

Also in the ~/Models/AccountModel.cs file you might have the following context defined by default:

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

    public DbSet<UserProfile> UserProfiles { get; set; }
}
相关阅读:
Top