我正在尝试清理我的web.config文件,因为连接字符串部门似乎有冗余。但是,我似乎无法将其简化为只有一个。
以下是我所拥有的: 这是数据库优先方案。
我的上下文代码:
public partial class InventoryMgmtContext : DbContext
{
public InventoryMgmtContext()
: base("name=InventoryMgmtContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
...
}
我在web.config中的连接字符串,对应于InventoryMgmtContext:
<connectionStrings>
<add name="InventoryMgmtContext" connectionString="metadata=res://*/InventoryMgmt.InventoryMgmt.csdl|res://*/InventoryMgmt.InventoryMgmt.ssdl|res://*/InventoryMgmt.InventoryMgmt.msl;provider=System.Data.SqlClient;provider connection string="Data Source=<SERVER_NAME>;initial catalog=<DB_NAME>;persist security info=True;user id=<UID>;password=<PWD>;MultipleActiveResultSets=True;Application Name=OTIS_InvnMgmt"" providerName="System.Data.EntityClient" />
</connectionStrings>
我希望这就是我所需要的,但后来我看到我还有以下连接字符串:
<add name="DefaultConnection" connectionString="Data Source=<SERVER_NAME>;Initial Catalog=OTIS;Persist Security Info=True;User ID=<UID>;Password=<PWD>;MultipleActiveResultSets=True;Application Name=OTIS_InvnMgmt" providerName="System.Data.SqlClient" />
我的web.config中的Membership和Role提供程序有以下内容,它们引用了DefaultConnection字符串:
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear />
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" connectionStringName="DefaultConnection" />
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear />
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="DefaultConnection" />
</providers>
</membership>
最后,初始化具有以下内容:
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);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
try
{
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: false);
//WebSecurity.InitializeDatabaseConnection("InventoryMgmtContext", "UserProfile", "UserId", "UserName", autoCreateTables: false);
}
}
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);
}
}
}
}
我将角色和成员资格提供程序更改为指向InventoryMgmtContext字符串,这样可行,但是如果我将WebSecurity.InitializeDatabase更改为使用InventoryMgmtContext(来自原始DefaultConnection),例如:
WebSecurity.InitializeDatabaseConnection("InventoryMgmtContext", "UserProfile", "UserId", "UserName", autoCreateTables: false);
我收到以下错误:
"Unable to find the requested .Net Framework Data Provider. It may not be installed."
我在整个项目中搜索过“DefaultConnection”,但无法找到引用的位置或方式。
我错过了什么?