How can i change the default database for simple membership tables

坚强是说给别人听的谎言 提交于 2019-12-01 20:30:00

SimpleMembership gets initialised in the InitializeSimpleMemberhsipAttribute.cs class.

In the default MVC 4 internet project template, navigate to the Filters directory, and in the above class you'll find the initialising method:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile",
 "UserId", "UserName", autoCreateTables: true);

That's the default setup, if you want to point it at a different database do something like this:

WebSecurity.InitializeDatabaseConnection("MyContext", "TableToPointTo", 
"UserIdColumn", "UserNameColumn", autoCreateTables: false);

In your context class:

public class MyContext : DbContext {...}

And change the name in the connection strings section inside the <configuration> section in the root web.config file:

<connectionStrings>
    <add name="MyContext" connectionString="..." />
</connectionStrings>

I have been looking into this thread since I was facing the same issue. I had tried all the steps like -

  • Granting permission to SQLServer
  • Removing the [InitializeSimpleMembership] attribute for account controller along with adding WebSecurity.InitializeDatabaseConnection connection in Global.asax.

But nothing worked.

So tried something else -

  • Changed the Data Source in the default connection string to localhost.
  • Created an empty DB in the localhost named as MVCApp
  • Set the initial catalog in the connection string to MVCApp
  • Removed the AttachDBFilename from the connection string

So now my connection string points to a local database. And it worked.

I am still not sure why the default implementation by MS that had been working just couple of days back is not working as of now. But at this point of time I am happy with work around.

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