7

我想知道 EF4 是否支持跨数据库关系?例如:

db1

Author
  Id
  Name

数据库2

Posts
  Id
  Content
  db1.Author.Id

理想情况下,我需要做什么才能在我的 ef4 模型中获得这种关系?

你们有什么想法吗?

谢谢

4

3 回答 3

5

在 Microsoft Connect中找到了这个条目,它回答了关于 EF 目前提供的支持的问题(实际上它还不支持)。

还在 Social MSDN 中找到了一个关于此问题的线程。

Stack Overflow 上的其他链接:

总之,唯一给定的替代方案是:

  1. 在 EF 中使用视图

  2. 改用 NHibernate

于 2010-05-10T13:43:01.190 回答
4

如果您的数据库支持同义词,您可以欺骗 EF 跨越多个数据库。我在这里写了怎么做。

基本上,您最终会得到每个数据库的 edmx 文件,以及将它们合并到单个 edmx 文件中的脚本。同义词用于通过使用实际表名从另一个数据库引用一个数据库,因此当您尝试从 database1 访问 database2.table 时,EF 不会出错。您仍然必须在 EF 模型中手动设置两个数据库之间的链接,但是一旦设置,即使您重新运行合并脚本,它们也会保留。

设置同义词和合并 edmx 文件的脚本发布在链接中

于 2011-05-27T16:36:38.360 回答
3

I recently began a project that uses entity framework with two databases, one Oracle and one SQL Server. I could not find any information regarding cross-database or multiple database support in the entity framework.

Most posts from the MS Entity framework team are a couple of years old and indicate that including two databases in a single model is not a feature that will be included soon. I would be interested in having a concrete answer on whether it was included in 2010 myself although I suspect the answer is no.

Currently out project gets around this limitation by having a separate entity model for each database. This has solved the problem for the majority of the scenarios we've encountered thus far in the project.

In cases where we've needed to query the data from the two databases at the same time, we simply created a view in one or the other databases. Since we're using Oracle and SQL Server, this view would utilize either a Linked Server (SQL) or a DBLink (Oracle).

The disadvantage of views in the entity framework is we've had to spent more time than I expected getting the primary keys working.

Hope this helps.

于 2010-05-03T14:39:19.850 回答