简体   繁体   English

使用 SQL 服务器在一个 Entity Framework Core 查询中使用多个大型数据库?

[英]Use multiple huge databases in one Entity Framework Core query with SQL Server?

I want to query multiple tables from different databases in one single query with 1 or 2 dbcontext .我想使用 1 或 2 个dbcontext在一个查询中查询来自不同数据库的多个表。 I've been searching for some info, and it seems not easy to make it work perfectly.我一直在寻找一些信息,要让它完美运行似乎并不容易。

I need to make several queries with 5 or 6 tables from 3 or 4 different databases at the same time.我需要同时对来自 3 个或 4 个不同数据库的 5 个或 6 个表进行多个查询。

  • I've tried using the implementation of a dbcontext into the another but I'm having a problem with the primary keys which I don't have if I only use one database.我已经尝试将dbcontext的实现用于另一个,但如果我只使用一个数据库,我会遇到主键问题。
  • I've tried 2 dbcontext separately, but I'm getting an error:我分别尝试了 2 个dbcontext ,但出现错误:

Cannot use multiple DbContext instances within a single query execution message.不能在单个查询执行消息中使用多个 DbContext 实例。

I've read that I can relate the 2 databases, the thing is I cannot do that because of the structure of my enterprise, and it doesn't allow me to.我读过我可以关联这两个数据库,但由于我的企业结构,我不能这样做,而且它不允许我这样做。

This is in Entity Framework Core 2.2 or 3, I already tried both.这是在 Entity Framework Core 2.2 或 3 中,我已经尝试过。 I have already the databases created, so I scaffolded them.我已经创建了数据库,所以我搭建了它们。

I have a single example, with only 2 databases:我有一个示例,只有 2 个数据库:

var data =
   from j in jsa.TbEmp
   join i in info.TbcEmpGral on j.fiEmp equals i.fiEmp
   where i.fiEmp == 361591
   select i.FcName.ToString() + " - " + i.fiDate.ToString();

I want to query multiple tables from different databases [on one server] in one single query我想在一个查询中查询来自不同数据库 [在一台服务器上] 的多个表

The simplest way to do this is to create views or synonyms for all the tables in a single database.最简单的方法是为单个数据库中的所有表创建视图或同义词。 EF won't be able to scaffold the model automatically, but if you create the model by-hand EF will simply send SQL to that single database as if it contained all the tables, and SQL Server will redirect the queries to the appropriate objects through the synonyms or views. EF won't be able to scaffold the model automatically, but if you create the model by-hand EF will simply send SQL to that single database as if it contained all the tables, and SQL Server will redirect the queries to the appropriate objects through同义词或观点。

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

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