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

后端 未结 2 1947
礼貌的吻别
礼貌的吻别 2020-12-14 17:06

I have an MVC5 app that\'s using EF. I would like to add ASP.NET Identity and I\'ve noticed that the connection string for ASP.NET identity is using \"DefaultConnection\". W

相关标签:
2条回答
  • 2020-12-14 17:19

    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

    0 讨论(0)
  • 2020-12-14 17:39

    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)

    0 讨论(0)
提交回复
热议问题