我已明确设置AccountsContext以使用mytestdb
作为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);
}
}
这是带有注释部分的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>-->
这方面的任何帮助都会有所帮助。
答案 0 :(得分:4)
查看SimpleMembershipInitializer
文件中的~/Filters/InitializeSimpleMembershipAttribute.cs
私有类:
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; }
}