简体   繁体   English

多个连接字符串和实体框架

[英]Multiple connection strings and Entity Framework

I've been dabbling a bit recently with Entity Framework 5.0. 最近,我一直在研究Entity Framework 5.0。 The application that I'm writing currently uses a database on a development/testing server. 我正在编写的应用程序当前使用开发/测试服务器上的数据库。 Once the application is complete, though, it should be able to connect to multiple instances of the database on different servers. 但是,一旦应用程序完成,它就应该能够连接到不同服务器上的数据库的多个实例。 Basically, the same database will be on several different serves. 基本上,相同的数据库将位于多个不同的服务器上。 They have the exact same layout, just different information contained within. 它们具有完全相同的布局,只是其中包含的信息不同。

The user should be able to select which server they will access from a dropdown on the form. 用户应该能够从表单的下拉列表中选择要访问的服务器。 Using their choice, the program will connect to the correct database server. 使用他们的选择,程序将连接到正确的数据库服务器。

So, I'm wondering what caveats I should keep in mind while attempting this? 因此,我想知道在尝试此操作时应注意哪些注意事项? Is it feasible/doable to let the program choose a different database/server based on user input? 让程序根据用户输入选择其他数据库/服务器是否可行/可行? Will I have any special considerations because of using EF 5.0? 由于使用EF 5.0,我是否有任何特殊考虑?

So long as you can guarantee the database layout is identical - and having non-sync'd data is intended, you should have no problems with this. 只要您可以保证数据库布局是相同的-并且打算使用不同步的数据,那么您应该不会有任何问题。

I believe you will have to add a new constructor for your context using a partial class that will accept the connection string. 我相信您将必须使用将接受连接字符串的局部类为上下文添加新的构造函数。

public partial class MyContext
{
    public MyContext(string connectionstring) : base(connectionstring){}
}

And that should do it. 那应该做到的。

Make sure to use an Entity Framework friendly connection string. 确保使用实体框架友好的连接字符串。 See http://msdn.microsoft.com/en-us/library/vstudio/cc716756(v=vs.100).aspx 参见http://msdn.microsoft.com/zh-cn/library/vstudio/cc716756(v=vs.100).aspx

Using different connection strings is frequently done to target different development / production databases. 经常使用不同的连接字符串来针对不同的开发/生产数据库。 I've never run across your usage - but it should function just the same. 我从未遇到过您的用法-但它的功能应该相同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM