简体   繁体   English

更改数据库第一个实体模型的连接字符串

[英]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个配置文件需要更改。

  1. Web.config in the Website project (references and uses #2) 网站项目中的Web.config(参考和使用#2)
  2. App.config in the DataAccess project (references and uses #3) DataAccess项目中的App.config(参考和使用#3)
  3. App.config in the DatabaseModel project (contains the entity model) DatabaseModel项目中的App.config(包含实体模型)

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=&quot;data source=...connection string info...;multipleactiveresultsets=True;application name=EntityFramework&quot;" 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=&quot;data source=...connection string info...;multipleactiveresultsets=True;application name=EntityFramework&quot;" 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.

相关问题 如何在实体框架数据库第一个模型连接字符串中动态包含工作站ID? - How to include work station id in entity framework database first model connection string dynamically? 连接字符串,以同时使用“模型”和“ DB”用于同一数据库, - connection string to use both Model first, DB first for same database, SQL Server mdf文件的ASP.NET实体框架代码优先连接字符串,它在哪里以及如何更改它? - ASP.NET Entity Framework code-first connection string for SQL Server mdf file, where is it and how do I change it? 实体框架代码优先-具有两个版本的代码的模型变更(模型变更前和模型变更后) - Entity Framework Code First - Model Change with two versions of code (pre model change and post model change) 实体框架代码优先处理连接字符串配置 - Entity Framework code first approach connection string configuration 根据数据库优先实体模型验证条目长度 - Validate entry length based on database-first entity model 首先在实体框架模型中设计“热与否”样式数据库 - Designing “hot or not” style database in Entity Framework Model First 更改连接字符串以使用外部MSSQL数据库 - Change connection string to use external MSSQL database 如果连接字符串在代码端,如何更新实体 Model? - How to update Entity Model If Connection String is at Code Side? 如何在多个实体数据模型之间共享连接字符串 - How to share a connection string between multiple entity data model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM