[英]Change the connection string of the database first entity model
I have an ASP.NET website that uses the Entity Framework 6. I'm using the Database First method for creating the Model and use EntitesToDTOs to create DTOs for all the tables. 我有一个使用Entity Framework 6的ASP.NET网站。我正在使用数据库优先方法创建模型,并使用EntitesToDTOs为所有表创建DTO。 Now I need to be able to easily change the database it uses.
现在,我需要能够轻松更改其使用的数据库。 The databases are basically copies of each other, however they are separate from each other with different connection strings.
这些数据库基本上是彼此的副本,但是它们是彼此分开的,具有不同的连接字符串。
Right now I'm able to switch out the databases by manually changing the connection strings in the config files. 现在,我可以通过手动更改配置文件中的连接字符串来切换数据库。 There are 3 config files needing to be changed.
有3个配置文件需要更改。
The following section of each config file is manually modified. 每个配置文件的以下部分都是手动修改的。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<connectionStrings>
<!-- Entity Framework Connection Strings - Uncomment the one in use -->
<!-- Entities Database 1 -->
<add name="ClientEntities" connectionString="metadata=res://*/ClientModel.csdl|res://*/ClientModel.ssdl|res://*/ClientModel.msl;provider=System.Data.SqlClient;provider connection string="data source=...connection string info...;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
<!-- Entities Database 2 -->
<!--<add name="ClientEntities" connectionString="metadata=res://*/ClientModel.csdl|res://*/ClientModel.ssdl|res://*/ClientModel.msl;provider=System.Data.SqlClient;provider connection string="data source=...connection string info...;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />-->
</connectionStrings>
</configuration>
Is there a way to automate this process or do everything programmatically, perhaps based off the regular connection string in the web.config file? 是否有一种方法可以自动执行此过程或以编程方式执行所有操作,也许基于web.config文件中的常规连接字符串?
This is the regular connection string in the Web.config file and it reflects the entities database that needs to be used. 这是Web.config文件中的常规连接字符串,它反映了需要使用的实体数据库。
<add name="Database1" connectionString="..." providerName="System.Data.SqlClient" />
It sounds as if you want to have different build configurations for different scenarios. 听起来好像您想针对不同的场景使用不同的构建配置。 You can easily manage this for web using Web.config transformations and the same procedure is applicable to other .NET projects by using this tutorial .
您可以使用Web.config转换轻松地针对Web进行管理,并且通过使用本教程 ,相同的过程适用于其他.NET项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.