[英]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="data source=(local);initial catalog=GC_BranchName;user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=(local);initial catalog=GC_BranchName_1;user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework"" 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_1
和GC_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.dll
和using 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.