C# 如何连接来自两个不同数据库的两个表?

C# 如何连接来自两个不同数据库的两个表?,c#,sql,sql-server,entity-framework,linq,C#,Sql,Sql Server,Entity Framework,Linq,我有一个LINQ查询,它在两个数据库之间执行连接(oContext和oCommonContext): Logs=来自oContext.TbHistoryLog中的h 在h.FormName上的ocCommonContext.TbObjects中加入o等于o.ObjectRealName 在h.UserId等于u.UserId的oCommonContext.TbUsers中加入u 其中userIds.Contains((Guid)h.UserId)和&formNames.Contains(o.Ob

我有一个LINQ查询,它在两个数据库之间执行连接(
oContext
oCommonContext
):

Logs=来自oContext.TbHistoryLog中的h
在h.FormName上的ocCommonContext.TbObjects中加入o等于o.ObjectRealName
在h.UserId等于u.UserId的oCommonContext.TbUsers中加入u
其中userIds.Contains((Guid)h.UserId)和&formNames.Contains(o.ObjectRealName)
&&h.DateUpdated>fromDate&&h.DateUpdated
但当我运行它时,它会抛出一个错误:

指定的LINQ表达式包含对以下查询的引用: 与不同的上下文相关

我知道不允许在一个查询中引用两个数据库

我尝试将此作为一种解决方法:

    var histories = oContext.TbHistoryLog.ToList();

    Logs = from h in histories.AsQueryable()
           join o in oCommonContext.TbObjects on h.FormName equals o.ObjectRealName
           join u in oCommonContext.TbUsers on h.UserId equals u.UserId
           where userIds.Contains((Guid)h.UserId) && formNames.Contains(o.ObjectRealName)
           && h.DateUpdated > fromDate && h.DateUpdated < toDate
           orderby h.DateUpdated
           select new HistoryLog
           {
               Id = h.Id,
               ObjectId = h.ObjectId,
               UserName = Helper.CurrLang == Helper.SystemLanguage.Arabic ? u.UserAName : u.UserEName,
               UserCode = u.UserCode,
               DateUpdated = h.DateUpdated,
               ObjectCode = h.ObjectCode,
               FormName = h.FormName,
               ObjectName = Helper.CurrLang == Helper.SystemLanguage.Arabic ? o.ObjectAName : o.ObjectEName,
               State = h.State
           };

    return Logs.ToList();
var histories=oContext.TbHistoryLog.ToList();
Logs=历史记录中的h.AsQueryable()
在h.FormName上的ocCommonContext.TbObjects中加入o等于o.ObjectRealName
在h.UserId等于u.UserId的oCommonContext.TbUsers中加入u
其中userIds.Contains((Guid)h.UserId)和&formNames.Contains(o.ObjectRealName)
&&h.DateUpdated>fromDate&&h.DateUpdated
并引发一个不同的异常:

此方法支持LINQ to Entities基础架构,不支持 旨在直接从代码中使用


目前,我似乎在代码中没有选项,我认为最好用SQL创建视图,但问题是数据库名称未知,并且它们根据客户端首选项而不同,因此如果我在第一个数据库中创建视图,我不知道第二个数据库的名称,反之亦然。

AsQueryable()
历史记录中根本不需要使用
。AsQueryable()
。当使用不同的上下文时,至少一个上下文数据必须在内存中。可能有更好的方法,但如果两个数据库位于同一实例上,则可以创建一个视图来查询另一个数据库中的表(也可以使用远程实例)。然后你可以简单地使用EF中的视图。你有两个选项。如果您想使用2个上下文,那么您必须返回内存中的所有数据并在那里进行连接。或者在sql server中为远程表创建同义词,并在ef上下文中将实体添加到synony中。
    var histories = oContext.TbHistoryLog.ToList();

    Logs = from h in histories.AsQueryable()
           join o in oCommonContext.TbObjects on h.FormName equals o.ObjectRealName
           join u in oCommonContext.TbUsers on h.UserId equals u.UserId
           where userIds.Contains((Guid)h.UserId) && formNames.Contains(o.ObjectRealName)
           && h.DateUpdated > fromDate && h.DateUpdated < toDate
           orderby h.DateUpdated
           select new HistoryLog
           {
               Id = h.Id,
               ObjectId = h.ObjectId,
               UserName = Helper.CurrLang == Helper.SystemLanguage.Arabic ? u.UserAName : u.UserEName,
               UserCode = u.UserCode,
               DateUpdated = h.DateUpdated,
               ObjectCode = h.ObjectCode,
               FormName = h.FormName,
               ObjectName = Helper.CurrLang == Helper.SystemLanguage.Arabic ? o.ObjectAName : o.ObjectEName,
               State = h.State
           };

    return Logs.ToList();