I have implemented in the Add() method of my generic EF repository a check whether the row I’m going to insert already exists in the table, if it does, update it with the currently available info.
private List<T> _previousEntries;
//Try and find one with same PK in the temporary tables...
var previousRow = _previousEntries.Single(n => (int)n.GetType().GetProperty(_PKName).GetValue(n, null) == entPKValue);
//If we did find one...
if (previousRow != null)
{
//Update the row...
return;
}
//Add it...
So I know, I’m using reflection, which is slow but I have not found another way since different entities have different SQL PK names.
But I’m not sure that reflection is the biggest issue here, sometimes, _previousEntries will hold up to 800,000 items.
_previousEntries has its items assigned to it in the class constructor of the repository class. _PKName is also assigned a value in the class constructor depending on the type of T.
If I just set a breakpoint on the Single() statement, it can be processing for 2-3 seconds for sure so I don’t know how I could determine what is the bottleneck here: reflection or Single() on 800,000 items… It sure goes way faster on a 5,000 items list.
Any opinions ? Is there anything I can do to optimize my List ?
Provide a primary key accessor as a delegate
You would create a repositiory like this