我正在两个不同的数据库之间导入数据(它们具有不同的上下文)。
所以我有两个不同的背景。目标是将上下文A的一些数据导入到上下文B。
上下文 B 中的数据永远不会直接编辑,它们仅从上下文 A 导入。在上下文 B 中,我复制了从中导入数据的 ID。
现在我正在尝试检索不在上下文 B 中或具有更新版本的所有数据的列表。
我在两个表中有一个“ModifiedAt”字段,让我知道该字段是否已被修改。
这是我当前的代码:
//Here I get all my current data in the context B with their modification time
Dictionary<int,DateTime> currentItems = contextB.Dossiers.ToDictionary(d=>d.MatchingReferenceId, d=>d.ModifiedAt);
//And here the pain starts:
contextA.Dossiers.Where(da=> !currentItems.Keys.Contains(da.Id) || currentItems.FirstOrDefault(db=>db.Key == da.Id).Value <da.ModifiedAt)//And I'm looping on it with a foreach.
第一部分(我检查上下文 B 是否有该元素)有效,在第二部分中,我得到了这个异常:
Unable to process the type 'System.Collections.Generic.KeyValuePair`2[]', because it has no known mapping to the value layer.
但我不能做更简单的事情来在 Id 和修改时间之间建立这种链接(一开始,我有一个来自其他上下文的 POCO 类,我也尝试过使用匿名类型,结果相同)
我缺少什么?
Edit 1
我也尝试过,结果完全相同:
contextA.Dossiers.Where(da=> !currentItems.Keys.Contains(da.Id) || currentItems.Any(db=>db.Key == da.Id && db.Value
Edit 2
我尝试了 lambda,但这里它不喜欢同时处理两个上下文:
var myList = (from db in contextB.Dossiers
let dstId = newContext.Dossiers.Select(d=>d.MatchingReferenceId)
from da in contextA.Dossiers
where !db.Contains(dSource.ID)|| (db.MatchingReferenceId == da.Id && db.ModifiedAt< da.ModifiedAt)
select new {NewId =db.Id, OldId = da.Id});
-->
指定的 LINQ 表达式包含对以下查询的引用
与不同的上下文相关。