实体框架在运行时多个数据库(同一模式)? [英] Entity Framework to multiple databases (same schema) at runtime?

查看:129
本文介绍了实体框架在运行时多个数据库(同一模式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,请允许我,我很新的EF。随着中说,这是我的困境:

First of all, let me state I'm very new to EF. With that said, here's my dilemma:

将有一个ASP.NET应用程序迁移到ASP.NET MVC的。我想利用EF这一点。这里是存储客户信息一主数据库。除此之外,每一个客户都有自己的数据库。这些都是我们有约束。

There will be an ASP.NET App migrated to ASP.NET MVC. I would like to utilize EF for this. There is one main database which stores "client information". Apart from that, every "client" has their own database. These are the constraints we have.

目前,在主数据库,使我建立每个客户端的连接字符串,使单个SQL调用客户端的信息。

Currently, client information in the main DB that enables me to build a connection string per client and make individual SQL calls.

我将如何实现在实体框架是一回事吗?每个数据库都会有相同的模式。有没有一种方法以编程方式切换连接字符串?这些数据块目前在同一台服务器上,但是这不是必需的,它可能是一个完全不同的服务器。

How would I accomplish the same thing in Entity Framework? Each database WILL have the same schema. Is there a way to programmatically switch the Connection String? These DBs are currently on the same server, but that's not a requirement and it may be a completely different server.

任何想法?

在Web.config多个连接字符串将是最后的手段。即使是这样,我不知道究竟如何这样组装起来。

Multiple connection strings in the Web.config would be a last resort. Even then, I'm not sure how exactly to wire this up.

感谢您提前。

推荐答案

如果您通过您的实体构造一个EntityConnection对象的工作,你可以很容易地更改数据库pretty。

If you work through an EntityConnection in the constructor of your entities object, you can change the database pretty easily.

EntityConnection con = new EntityConnection(connString);
con.ChangeDatabase(dbName);
using (Entities context = new Entities(con))
{
    // Some code here
}

这篇关于实体框架在运行时多个数据库(同一模式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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