1

我正在尝试清理我的 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=&quot;Data Source=<SERVER_NAME>;initial catalog=<DB_NAME>;persist security info=True;user id=<UID>;password=<PWD>;MultipleActiveResultSets=True;Application Name=OTIS_InvnMgmt&quot;" 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);
                }
            }
        }
    }

我将 Role 和 Membership Provider 改为指向 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”,但找不到引用它的位置或方式。

我错过了什么?

4

0 回答 0