asp.net-mvc 如何设置 ASP.NET Identity 以使用我自己的连接字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19813374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I setup ASP.NET Identity to use my own connection string?
提问by ShaneKm
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 MVC5Entitiesand NOT DefaultConnection => (LocalDb)\v11.0??
thanks
我有一个使用 EF 的 MVC5 应用程序。我想添加 ASP.NET 标识,我注意到 ASP.NET 标识的连接字符串使用“DefaultConnection”。我需要做什么才能在我现有的数据库(源 = 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="data source=DILS-S1301;initial catalog=MVC5;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=DILS-S1301;initial catalog=MVC5;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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'.
采纳答案by jd4u
The actual question shall be "How do I setup ASP.NET Identity to use my own connection string?"
实际问题应该是“如何设置 ASP.NET Identity 以使用我自己的连接字符串?”
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:EntityFramework 的默认连接字符串可以使用 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)
选项 2:以编程方式,您还可以将 ConnectionString 名称传递给 DbContext 的构造函数。喜欢new ApplicationDbContext(MyConnectionString)
回答by Robert Noack
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-and-Visual-Studio-2013.aspx

