I'm in need of the functionality to retrieve a tracked entity by key without a SQL call to the DB. So I need something like the .Find
but if it's not tracked NULL
is returned.
The reason why I need this is because following code throws:
public void Test()
{
var id = 5;
var a = new Entity { Id = id };
_ctx.Entry(a).State = EntityState.Added;
var b = new Entity { Id = id };
var bEntry = _ctx.Entry(b);
// Delete entity
// When it was already added in this 'lifetime' detach it again (since it's not saved in the DB yet)
if (bEntry.State == EntityState.Added)
bEntry.State = EntityState.Detached;
else
bEntry.State = EntityState.Deleted;
var c = new Entity { Id = id };
var cEntry = _ctx.Entry(c);
// Add entity
// When it was already deleted in this 'lifetime' detach it again (since it's not removed in the DB yet)
if (cEntry.State == EntityState.Deleted)
cEntry.State = EntityState.Detached;
else
cEntry.State = EntityState.Added;
var d = new Entity { Id = id };
var dEntry = _ctx.Entry(d);
}
If you would never create new entities but always use a
. This code will work.
The reason why it doesn't work now, is because internally EF tracks the entities in a dictionary using a 'by ref compare' (https://github.com/dotnet/efcore/blob/9ac01d6035c76626d89aa1a3cd8d200db2c3c0e1/src/EFCore/ChangeTracking/Internal/EntityReferenceMap.cs#L103)
As a result the TryGetValue
EF uses will be false
and it will create a new entry with state Detached
. And later the code will crash because I try to add a entity that is already tracked.
This above problem would be solved if I could get the tracked entity since they would have the same ref.
But I don't seem to find a way to access the same code that happens in .Find()
.
I could in theory use the .Local
property or get it by using ChangeTracker.Entries()
but since this are just regular lists the performance to lookup the entry is taking way to long.
I know this is a very specific question. But I don't really seem to find anything. It's a bit a shame to give up since internally EF knows what I need.
Brecht