使实体框架模型跨越多个数据库

Making an Entity Framework Model span multiple databases(使实体框架模型跨越多个数据库)

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

问题描述

做诸如

CREATE SYNONYM [dbo].[MyTable] FOR [AnotherDatabase].dbo.[MyTable]

然后修改实体框架的 edmx 文件以像读取任何其他表一样读取此对象?

and then modify Entity Framework's edmx file to read this object like it would any other table?

我做了一个快速的示例测试,它似乎可以很好地进行选择和更新,但我想知道是否有任何理由为什么我不应该这样做

I did a quick sample test and it seems to work fine for selecting and updating, but I wanted to know if there was any reason why I shouldn't be doing this

我通过创建一个指向第二个数据库的 edmx 文件来获取表定义,在那里构建实体,然后将定义复制/粘贴到第一个数据库的 edmx 文件中.

I am getting the table definition by creating an edmx file pointing to the 2nd database, building out the entities there, then copy/pasting the definition into the 1st database's edmx file.

更新

如果有人感兴趣,我写了我做了什么使 edmx 文件跨越多个数据库 此处.它包括用于生成同义词和合并 edmx 文件的脚本.

If anyone is interested, I wrote up what I did to make an edmx file span mulitple databases here. It includes scripts for generating synonyms and merging edmx files.

推荐答案

如果你做了一个测试并且它有效,你可能展示了一些其他人不知道的东西.直到现在我总是回答这种类型的问题:不可能对两个数据库使用单个模型(基于视图隐藏第二个数据库中的表的一些更丑陋的解决方法).现在我知道两种解决方法.

If you made a test and it worked you probably showed something nobody else know about. Till now I always answered this type of question: It is not possible to use single model with two databases (with some more ugly workaround based on views hiding tables from the second database). Now I know two workarounds.

这种方法的唯一缺点是,如果您运行从数据库更新模型,那么手动对 EDMX 的 SSDL 部分所做的所有更改都会丢失.这意味着要么手动开发 EDMX(这是一项非常艰巨的工作),要么使用一些工具/脚本在每次更新数据库后添加您的更改.

The only disadvantage of this approach is that all changes made manually to SSDL part of your EDMX are always lost if you run Update model from database. This means either manual development of EDMX (which is quite hard work) or using some tool / script which will add your changes after each update from database.

这篇关于使实体框架模型跨越多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使实体框架模型跨越多个数据库

基础教程推荐