简体   繁体   English

我可以使用一个连接字符串在Entity Framework中使用具有相同表结构的多个数据库吗?

[英]Can I use one connection string to use multiple databases with the same table structure in Entity Framework?

I have a web api application and it has its own database. 我有一个Web API应用程序,它有自己的数据库。 The web api app distributes data to its branches' database. Web API应用程序将数据分发到其分支机构的数据库。 The branch databases are all same in table structure but each database has its own connection string in the web.config 分支数据库的表结构都相同,但是每个数据库在web.config都有自己的连接字符串

<add name="Branch" connectionString="metadata=res://*/Models.HOGC.csdl|res://*/Models.HOGC.ssdl|res://*/Models.HOGC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=GC_BranchName;user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="Branch_1" connectionString="metadata=res://*/Models.HOGC.csdl|res://*/Models.HOGC.ssdl|res://*/Models.HOGC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=GC_BranchName_1;user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I am using Entity framework Database First and I route my data using dbcontext 我正在使用Entity Framework Database First,并且使用dbcontext路由数据

public HOGCEntities(string connString)
        : base("name=" + connString)

I have a list of branches in my main database table. 我的主数据库表中有一个分支列表。 It contains the name of the connection string and the name of the database/catalog ( Branch_1 & GC_BranchName_1 ). 它包含连接字符串的名称和数据库/目录的名称( Branch_1GC_BranchName_1 )。

I want to know is, can I use just one connection string instead of manually or programmatically creating another connection string with the same attributes? 我想知道的是,我可以只使用一个连接字符串,而不是手动或以编程方式创建具有相同属性的另一个连接字符串吗? Like looping through my branch table to route my data? 是否喜欢遍历分支表以路由数据?

"How can I send postcards to several people living at different places by using the same address?" “如何使用同一地址将明信片发送给居住在不同地方的几个人?”

Well, you can't. 好吧,你不能。

The connection string is the "address" of a database and if you have several databases you need a specific connection string for each one of them. 连接字符串是数据库的“地址”,如果您有多个数据库,则每个数据库都需要一个特定的连接字符串。

However, if the databases have the same structure, you can use the same entity model and the same business classes for all of them. 但是,如果数据库具有相同的结构,则可以对所有数据库使用相同的实体模型和相同的业务类。


Configuration settings 配置设定

You can use the same connection string setting in your web.config , if you provide a template connection string with a placeholder for the catalog name: 如果为目录名称提供带占位符的模板连接字符串,则可以在web.config中使用相同的连接字符串设置:

<add name="Branch"
   connectionString=
       "...provider=...;data source=(local);initial catalog={0};..." providerName="..." />

where "{0}" is the placeholder. 其中“ {0}”是占位符。

public HOGCEntities(string catalog)
    : base(String.Format(ConfigurationManager.ConnectionStrings["Branch"].ConnectionString,
                         catalog)
          )

You will also need a reference to the assembly System.Configuration.dll and a using System.Configuration; 您还需要引用程序集System.Configuration.dllusing System.Configuration;的引用using System.Configuration; .


Some databases allow you to create database links. 一些数据库允许您创建数据库链接。 By using them, you could link satellite databases to a master database. 通过使用它们,您可以将卫星数据库链接到主数据库。 But this leads to the problem of accessing the different tables with the same name. 但这会导致访问具有相同名称的不同表的问题。 I don't know how the same business class could be mapped to the different tables in turn. 我不知道如何将相同的业务类依次映射到不同的表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在一个应用程序中为2个数据库使用相同的实体模型吗? - Can I use the same Entity Model for 2 databases in one application 如何在项目中使用多个实体框架连接字符串? - How can I use multiple entity framework connection strings in a project? 如何创建临时表并在与实体框架相同的连接中使用它? - How to create a temporary table and use it in the same connection with Entity Framework? 使用 SQL 服务器在一个 Entity Framework Core 查询中使用多个大型数据库? - Use multiple huge databases in one Entity Framework Core query with SQL Server? 实体框架:将多个相似的类映射到相似数据库中的一个表 - Entity Framework: map multiple similar classes to one table in similar databases 我可以将SqlCredential与Entity Framework一起用于SQL连接吗? - Can I use SqlCredential with Entity Framework for the SQL connection? 对于一个EDMX文件,使用与多个数据库相关的多个连接字符串 - With one EDMX file use multiple connection strings that relate to multiple databases 实体框架:如何使用多个上下文? - Entity framework: How can I use more then one context? 我可以在Entity Framework中合并来自多个数据库的对象吗? - Can I union an object from multiple databases in Entity Framework? 如何对具有相同结构的动态表使用Entity Framework 6 LINQ查询? - How to use Entity Framework 6 LINQ query for Dynamic tables with same structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM