Setup a valid connection string for SimpleMembership

a 夏天 提交于 2019-12-24 12:34:45

问题


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

Here's my connection string:

<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" />

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);
            }
        }
    }
}

Where Database_Entities2 is:

<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" />

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!

EDIT

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" />

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.


回答1:


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" />



回答2:


If you use Entity Framework the connection string would be created by the IDE. And from your description i guess you created that connection string by your own, not via IDE.

So the IDE would place something like this in your 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" />

To create the connection in your program you could use this:

libraryEntities1 dbconn = new libraryEnitits1();

Now you could handle the connections via dbcon object.



来源:https://stackoverflow.com/questions/21458638/setup-a-valid-connection-string-for-simplemembership

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!