EntityFramework: Retrieving conditional data in two different contexts

I am importing data between two different databases (which do not have the same context).

So, I have two different contexts. The goal is to import some context data A into context B.

Data in context B is never edited directly, it is imported only from context A. In context B, I copied the identifier from which it was imported.

Now I'm trying to get a list of all the data that is not in context B or has a newer version.

I have a changed field in two tables, which allows me to find out if the field has been changed.

Here is my current code:

//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.

The first part (where I check if context B has an element or not), with the second part I got this exception:

Unable to process the type 'System.Collections.Generic.KeyValuePair`2[]', because it has no known mapping to the value layer.

, Id ( POCO , , )

?

1

: contextA.Dossiers.Where(da = > ! currentItems.Keys.Contains(da.Id) || currentItems.Any(db = > db.Key == da.Id & & db.Value

2

, :

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 , .

+5
1

, .

, . SQL. , SQL.

, -, kvp SQL. POCO - .net-, .

, , .Contains() / , . :

List<int> someIds = ...
var result = context.Data.Where(d => someIds.Contains(d.Id)).ToList();

, --.

+10

All Articles