使用实体框架与多个MS SQLSERVER数据库 [英] Using Entity FrameWork with multiple MS SQLSERVER Databases

查看:160
本文介绍了使用实体框架与多个MS SQLSERVER数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜查来回但似乎不能得到什么,我需要保持。我很抱歉,如果这已经回答了晚期。重定向到的讨论对我有好处。

I have searched back and forth but seemingly could not get a hold of what I need. I am sorry if this has been answered of late. A redirection to the discussion will do me good.

这是该方案。我已经被我的老板指示,从Microsoft Visual FoxPro中移动(MS吸是来支持2015年)到.NET C#。对于良好的基础,并采用最佳做法的缘故,我决定先共同学习,片相关的信息,然后开始编码。这是第二年。

This is the scenario. I have been instructed to move from Microsoft Visual Foxpro (MS is withdrawing support come 2015) to .Net C# by my boss. For the sake of good foundation and adoption of best practices, I have decided to first learn, piece pertinent information together, then start coding. This is the second year.

我们是一局的公司,提供工资单处理超过50个客户端外包服务。每个客户端目前拥有自己的数据库。该数据库有完全一致的结构表。

We are a bureau company that offer payroll processing outsource services to over 50 clients. Each client currently has their own database. The databases have tables with completely identical structures.

我是一个新手。全新到.NET的世界。

I am a newbie. Totally new to .net world.

我已经使用数据表,的DataReader开始了与原始SQL,但在我的研究,我得到了一些讨论劝阻这一点。许多人认为实体框架应该达到目的的观点。但一个是允许尤其是在复杂的查询涉及的方法混合。

I had started off with raw SQL using datatables, datareaders but in my research I got some discussions discouraging this. Many were of the view that Entity Framework should serve the purpose. But one is allowed to mix approaches especially when complex queries are involved.

有人能指出我的一些'好读,我可以有超过50张玉峰数据库实现实体框架。每个数据库是完全独立的,并已完全dowith任何其他。当用户登录,他们选择他们需要处理的,那么EF指向数据库薪资的客户端。

Can someone point me to some 'good read' where I can implement Entity Framework with over 50 indentical databases. Each database is totally independent and has nothing to dowith any other. When the user logs in, they select which client they need to process payroll for, then EF points to that database.

推荐答案

EF需要2个不同的信息与数据库中的数据来工作:

EF needs 2 different pieces of information to work with data from a database:

1)的数据库模式:这是包括作为在应用程序的编译代码并且通常不能在改变。运行时

1) The database schema: This is included as compiled code in your application and cannot normally be changed at runtime.

2)的连接字符串:这是在运行时提供,通常是从一个配置文件

2) The connection string: This is provided at runtime, normally from a config file.

在你的情况下,所有的数据库具有相同的模式,所以你可以模拟一个数据库,它将为所有其他工作。

In your case, all the databases have the same schema, so you can just model one database and it will work for all the others.

您想改变这件作品连接字符串。这告诉EF如何找到数据库,可以在运行时提供。

The piece you want to change is the connection string. This tells EF how to find the database and can be provided at runtime.

有是过载的的DbContext 构造函数将连接字符串作为参数: MSDN:的DbContext构造函数(String)

There is an overload of the DbContext constructor which takes a connection string as a parameter: MSDN: DbContext Constructor (String)

和有即使在框架类,有助于创建连接为你的字符串:

And there are even classes in the framework that help create connection strings for you:

MSDN:EntityConnectionStringBuilder类

MSDN:连接字符串生成器

这篇关于使用实体框架与多个MS SQLSERVER数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆