How do I setup ASP.NET Identity to use my own connection string?

走远了吗. 提交于 2019-11-30 04:41:57

The actual question shall be "How do I setup ASP.NET Identity to use my own connection string?"

If above is the correct summary of your question, below is the answer.

ASP.NET Identity uses EntityFramework for database related tasks. So you can use following option as suitable.

Option 1: EntityFramework's default connection string can be setup using following code snippet in web.config

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>

Option 2: Programatically, you can also pass ConnectionString name to the DbContext's constructor. like new ApplicationDbContext(MyConnectionString)

Actually all you have to do is change the DefaultConnection connectionString to be your desired connection string. If you used the normal settings for a new MVC 5 project, and the identity tables do not exist in the new DB they will be created there automatically.

You do not have to edit the portion of the connection string and you do not have to edit the DbContext constructor unless you want to change the conntectionString name and are not OK with replacing the default connection string.

If you are OK with replacing the default connection string, you should be able to just replace it... this worked for me.

I found this article helpful as well: http://www.typecastexception.com/post/2013/10/27/Configuring-Db-Connection-and-Code-First-Migration-for-Identity-Accounts-in-ASPNET-MVC-5-and-Visual-Studio-2013.aspx

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