为SimpleMembership设置有效的连接字符串

时间:2014-01-30 14:05:14

标签: c# entity-framework connection-string sqlclient

我正在尝试为我的数据库设置一个有效的连接字符串,以便我的WebSecurity.InitializeDatabaseConnection方法可以指向我的数据库。

这是我的连接字符串:

<add name="Database_Entities1" connectionString="metadata=res://*/Models.DAL.Entities.MTG_Model.csdl|res://*/Models.DAL.Entities.MTG_Model.ssdl|res://*/Models.DAL.Entities.MTG_Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=**.**.***.***;initial catalog=Model_Name;persist security info=True;user id=*****;password=*********;multipleactiveresultsets=True;application name=EntityFramework;Pooling=True;Max Pool Size=1000&quot;" providerName="System.Data.EntityClient" />

此处的问题是提供商名称System.Data.EntityClient。我实际上需要它指向System.Data.SqlClient,但是当我尝试复制/粘贴连接字符串并将System.Data.EntityClient替换为其他提供者名称时,我会像Keyword not supported: 'metadata'.那样崩溃。

所以我的理解是调用我的方法:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
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()
        {
            Database.SetInitializer<Database_Entities1>(null);

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

                WebSecurity.InitializeDatabaseConnection("Database_Entities2", "UserProfile", "UserId", "UserName", 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);
            }
        }
    }
}

Database_Entities2的位置:

<add name="Database_Entities2" connectionString="metadata=res://*/Models.DAL.Entities.MTG_Model.csdl|res://*/Models.DAL.Entities.MTG_Model.ssdl|res://*/Models.DAL.Entities.MTG_Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=**.**.***.***;initial catalog=Model_Name;persist security info=True;user id=*****;password=*********;multipleactiveresultsets=True;application name=EntityFramework;Pooling=True;Max Pool Size=1000&quot;" providerName="System.Data.SqlClient" />

不起作用,因为SqlClient连接字符串不起作用。谁能帮助我实现我想要做的事情?非常感谢!

修改

另外两条信息:我有这个连接字符串:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MyApp-20130829080410;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MyApp-20130829080410.mdf" providerName="System.Data.SqlClient" />

哪个有效;但是,正如您所看到的,它并没有指向我的数据库,所以如果我尝试检索表“UserProfile”中的任何用户,那么由于该表是在本地数据库中创建的(我不知道)为什么我有),而不是主要的(Database_Entities1),因此这个问题。

2 个答案:

答案 0 :(得分:3)

SimpleMembershipProvider需要普通连接字符串,而不是实体框架连接字符串。您需要定义一个单独的连接字符串,该字符串指向与您的实体框架连接字符串相同的数据库并使用它:

<add name="MembershipDB" 
    connectionString="Data Source=**.**.***.***;Initial Catalog=Model_Name;[...]" 
    providerName="System.Data.SqlClient" />

答案 1 :(得分:-1)

如果使用Entity Framework,则IDE将创建连接字符串。根据您的描述,我猜您自己创建了该连接字符串,而不是通过IDE创建。

因此IDE会在你的app.config中放置这样的东西:

<add name="libraryEntities1" connectionString="metadata=res://*/privlibDBmodel.csdl|res://*/privlibDBmodel.ssdl|res://*/privlibDBmodel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=[yourserver];initial catalog=library;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

要在程序中创建连接,可以使用:

libraryEntities1 dbconn = new libraryEnitits1();

现在您可以通过dbcon对象处理连接。