如何设置ASP.NET MVC标识以使用我自己的连接字符串?

时间:2022-12-04 08:20:13

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". What Do I need to do so that ASP.NET Identity tables are created in my already existing db (source=DILS-S1301;initial catalog=MVC5;) as specified in MVC5Entities and NOT DefaultConnection => (LocalDb)\v11.0?? thanks

我有一个使用EF的MVC5应用程序。我想添加ASP.NET身份,我注意到ASP.NET身份的连接字符串使用“DefaultConnection”。我需要做什么才能在我已经存在的db(source = DILS-S1301;初始目录= MVC5;)中创建ASP.NET标识表,如MVC5Entities和NOT DefaultConnection =>(LocalDb)\ v11.0中所指定的那样? ?谢谢

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MySuperAwesomeMVCApp-20131105011429.mdf;Initial Catalog=aspnet-MySuperAwesomeMVCApp-20131105011429;Integrated Security=True" providerName="System.Data.SqlClient"      />
    <add name="MVC5Entities" connectionString="metadata=res://*/Models.Mvc5Model.csdl|res://*/Models.Mvc5Model.ssdl|res://*/Models.Mvc5Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DILS-S1301;initial catalog=MVC5;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

I tried modifying "DefaultConnection" like so:

我尝试修改“DefaultConnection”,如下所示:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=DILS-S1301;AttachDbFilename=|DataDirectory|\MVC5.mdf;Initial Catalog=MVC5;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="MVC5Entities" connectionString="metadata=res://*/Models.Mvc5Model.csdl|res://*/Models.Mvc5Model.ssdl|res://*/Models.Mvc5Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DILS-S1301;initial catalog=MVC5;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

but now i get an error:

但现在我收到一个错误:

Database 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MVC5.mdf' already exists. Choose a different database name.
Cannot attach the file 'C:\Users\blah\Documents\Visual Studio 2013\Projects\MySuperAwesomeMVCApp\MySuperAwesomeMVCApp\App_Data\MVC5.mdf' as database 'MVC5'.

2 个解决方案

#1


11  

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

实际问题应该是“如何设置ASP.NET身份以使用我自己的连接字符串?”

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.

ASP.NET Identity使用EntityFramework进行数据库相关任务。因此,您可以使用以下选项。

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

选项1:可以使用web.config中的以下代码片段设置EntityFramework的默认连接字符串

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

选项2:以编程方式,您还可以将ConnectionString名称传递给DbContext的构造函数。比如新的ApplicationDbContext(MyConnectionString)

#2


3  

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.

实际上,您所要做的就是将DefaultConnection connectionString更改为您想要的连接字符串。如果您使用新MVC 5项目的常规设置,并且新数据库中不存在标识表,则会自动在那里创建标识表。

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.

您不必编辑连接字符串的部分,也不必编辑DbContext构造函数,除非您想要更改conntectionString名称,并且替换默认连接字符串不正常。

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

我发现这篇文章也很有帮助:http://www.typecastexception.com/post/2013/10/27/Configuring-Db-Connection-and-Code-First-Migration-for-Identity-Accounts-in-ASPNET- MVC-5和视觉工作室,2013.aspx

#1


11  

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

实际问题应该是“如何设置ASP.NET身份以使用我自己的连接字符串?”

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.

ASP.NET Identity使用EntityFramework进行数据库相关任务。因此,您可以使用以下选项。

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

选项1:可以使用web.config中的以下代码片段设置EntityFramework的默认连接字符串

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

选项2:以编程方式,您还可以将ConnectionString名称传递给DbContext的构造函数。比如新的ApplicationDbContext(MyConnectionString)

#2


3  

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.

实际上,您所要做的就是将DefaultConnection connectionString更改为您想要的连接字符串。如果您使用新MVC 5项目的常规设置,并且新数据库中不存在标识表,则会自动在那里创建标识表。

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.

您不必编辑连接字符串的部分,也不必编辑DbContext构造函数,除非您想要更改conntectionString名称,并且替换默认连接字符串不正常。

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

我发现这篇文章也很有帮助:http://www.typecastexception.com/post/2013/10/27/Configuring-Db-Connection-and-Code-First-Migration-for-Identity-Accounts-in-ASPNET- MVC-5和视觉工作室,2013.aspx