问题描述
我刚刚创建了一个ASP.NET MVC 4安培;项目的WebAPI。从那以后,我已经添加了的.edmx
数据源项目。
I have just created an ASP.NET MVC 4 & WebAPI project. After that I have added .edmx
data source to project.
我有相同的架构多个数据库。动态我想用在EF提供默认的构造函数来替换连接字符串。
I have multiple databases with the same schema. Dynamically I want to replace connection string using default constructor provided in EF.
但 Model1.Designer.cs
,每次我得到错误,如时间与相同签名成员已经声明。
But in Model1.Designer.cs
, every time I get error like "Member with same signature already declared".
我无法解决这个问题。
I'm unable to solve this problem.
推荐答案
是的,它的作品!你只需要改变的是连接字符串
。
Yes, it works! All you need to change is the connection string
.
和我刚才测试了它为了满足自己的好奇心。
And I have just tested it in order to satisfy my own curiosity.
下面是我采取的步骤:结果
1.将现有的数据库
和创建一个模型
它。结果
2.创建一个新的空数据库。结果
3.在 SQL Management Studio中
右键单击第一个数据库 - >任务 - > 导出数据
。所有IT的数据导出到新创建的数据库。结果
4.从第二个数据库中删除一些记录。结果
5.写这个code:
Here are the steps that I took:
1. Take an existing database
and create a model
for it.
2. Create a new empty database.
3. In SQL Management Studio
right click the first database -> Tasks -> Export Data
. Export all it's data to the newly created database.
4. Remove some records from the second database.
5. Write this code:
TMS_MiscEntities db = new TMS_MiscEntities();
TMS_MiscEntities dbCopy = new TMS_MiscEntities();
dbCopy.Database.Connection.ConnectionString = db.Database.Connection.ConnectionString.Replace("initial catalog=TMS_Misc", "initial catalog=TMS_Misc_new");
Response.Write(string.Format("DB 1 records: {0}<br/>", db.ZipCodes.Count()));
Response.Write(string.Format("DB 2 records: {0}<br/>", dbCopy.ZipCodes.Count()));
6。检查结果:
6. Check results:
DB 1 records: 869164
DB 2 records: 868709
7。得出结论,它的工作原理:)
7. Conclude that it works :)
这是我的连接字符串的外观:
This is how my connection string looks:
<add name="TMS_MiscEntities" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=ws2008;initial catalog=TMS_Misc;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
这篇关于实体框架:多个数据库具有相同的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!