为SimpleMembership设置一个有效的连接字符串 [英] Setup a valid connection string for SimpleMembership

查看:165
本文介绍了为SimpleMembership设置一个有效的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to setup a valid connection string for my database so that my WebSecurity.InitializeDatabaseConnection method can point to my database.

这是我的连接字符串:

<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 与其他提供商名称,我收到像$ code>关键字不支持的这样的崩溃:'元数据'。。

The problem here is the provider name which is System.Data.EntityClient. I actually need it to point toward System.Data.SqlClient, but when I try to copy/paste the connection string and replace the System.Data.EntityClient with the other provider name, I get such crash like Keyword not supported: 'metadata'..

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

So my understanding is that calling my method like this:

[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连接字符串不能正常工作。任何人都可以帮助我找到我想要做的事情吗?非常感谢!

Does not work because an SqlClient connection string does not work that way. Can anyone help me achive what I'm trying to do? Thanks a lot!

编辑

两个附加信息:我有这个连接字符串:

Two additionnal piece of information: I have this connection string:

<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),因此这个问题。

Which was working; however, as you can see, it does not point toward my database, so if I try to retrieve any users in the table "UserProfile", it is not possible since the table was created in the local database (which I don't know why I have), and not in the main one (Database_Entities1), hence this question.

推荐答案

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

The SimpleMembershipProvider expects a normal connection string, not an entity framework connection string. You need to define a separate connection string that points to the same database as your entity framework connection string and use that:

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

这篇关于为SimpleMembership设置一个有效的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆