简体   繁体   English

使用实体框架将一张表映射到不同的数据库

[英]Map one table to a different database with Entity Framework

I'd like to combine multiple databases into one Entity Framework "object model".我想将多个数据库组合成一个实体框架“对象模型”。 I do not need to change databases dynamically at runtime.我不需要在运行时动态更改数据库。 This is a static configuration issue.这是一个静态配置问题。 It should look like this:它应该是这样的:

  1. Entity1 mapped to DB1 .dbo.Entity1 Entity1 映射到DB1 .dbo.Entity1
  2. Entity2 mapped to DB2 .dbo.Entity2 Entity2 映射到DB2 .dbo.Entity2

And I should be able to say:我应该能够说:

new XyzContext().Entities1
new XyzContext().Entities2

How can I configure Entity Framework to take note of my multiple databases?如何配置实体框架以记录我的多个数据库?

I do not want to create views or synonyms.我不想创建视图或同义词。 This question is about an EF only solution.这个问题是关于 EF 唯一的解决方案。 I'm using the latest EF version and can switch to any version.我使用的是最新的 EF 版本,可以切换到任何版本。

There is no way to do it directly with Entity Framework, the context is linked to a Database and you can't access tables directly outside that Database.实体框架无法直接执行此操作,上下文链接到数据库,您无法直接访问该数据库之外的表。 I don't know your situation, but if I were you first of all I would try to move that tables to my Database.我不知道你的情况,但如果我首先是你,我会尝试将这些表移到我的数据库中。

If this is not possible one workaround to do it's just create views to your external table.如果这是不可能的一种解决方法,它只是为您的外部表创建视图。 But be careful populating views, remember every time that you modify the schema of your external table, to recreate the view, and it's something that is easy break.但是要小心填充视图,记住每次修改外部表的架构以重新创建视图时,这很容易中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM