跨多个数据库的ADO.Net实体框架 [英] ADO.Net Entity Framework across multiple databases

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

问题描述

在Linq2Sql中,您只需将数据库名称添加到源中就可以将数据上下文连接到多个数据库.

在Linq 2实体/ADO.net实体框架中是否有实现此目的的方法?可以将数据库名添加到表名吗?

Is there a way to achieve this in Linq 2 Entities / ADO.net Entity Framework? Can the database name somewhere be added to the table name?

我试图将'Schema'从dbo更改为 MyDatabase.dbo ,但是EF将其封装到

I tried to change the 'Schema' from dbo to MyDatabase.dbo, but the EF encapsulates this into

SELECT FROM [MyDatabase.dbo].[MyTable]

诡计多端,就像将'Schema'更改为'MyDatabase].[dbo'将转义为

Trickery like changing 'Schema' to 'MyDatabase].[dbo' will be escaped into

SELECT FROM [MyDatabase]].[dbo].[MyTable]

(记住两个]].)

推荐答案

首先,您链接到LINQ to SQL的答案只是利用数据库服务器执行异类查询的能力.我不明白为什么这对于实体框架不起作用,因为它是数据库服务器功能,而不是任何一个框架的功能.换句话说,LINQ to SQL仍在处理连接,好像只涉及一个数据库服务器一样.但是请记住,并非所有的数据库服务器都可以做到这一点.

The answer you link to for LINQ to SQL is just using the ability of the DB server to do heterogeneous queries. I don't see why that wouldn't work for the Entity Framework, also, as it's a DB server feature, not a feature of either framework. In other words, LINQ to SQL is still dealing with the connection as if only one database server were involved. Bear in mind, however, that not all DB servers can do this.

关于EDMX中的更改,请在"SSDL内容"部分中查找EntitySet节点的Schema属性.

As far as what to change in the EDMX, look for the Schema attribute of the EntitySet node in the "SSDL content" section.

关于此技术的一个警告是,当您从数据库更新模型时,存储模型将被清除并从头开始替换.因此,您需要重新应用这些更改.这在LINQ to SQL中不是问题,因为LINQ to SQL根本不支持从数据库进行自动更新.

One caveat about this technique is that when you update your model from the database, the storage model is wiped out and replaced from scratch. So you would need to reapply these changes. This is not an issue in LINQ to SQL, because LINQ to SQL does not support automated updates from the database at all.

更好的选择可能是在数据库中创建一个VIEW,该VIEW引用另一个数据库并映射该视图,而不是直接映射表和另一个数据库.

A better alternative would probably be to create a VIEW in the database which referenced the other database and map that view instead of mapping the table and the other database directly.

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

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