You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This question is about what or whether there is a good way of telling the context to use only a particular instance of an immutable entity in the context, as it is producing an EF Core instance of entity type cannot be tracked error, I know there are a number of issues pertaining to this, but havn't seen a good solution on how to avoid this when using statically defined entities.
From reading and my (limited) understanding what is happening is some query pulls the static defined variable into the context (say AppointmentStatus.Finished in this case), such as when getting all the Appointments, so this become the instance known to the context, but when updating an appointment to AppointmentStatus.Finished from the static reference in AppointmentStatus, this becomes a second instance tracked by the context. EF then sees these as two separate instances for the same thing (even though they are the same, and immutable), as EF determines equivalency by reference equals.
What would be the best way of telling the context to only use one of the instances and ignore the other?
EF Core version: 5.0.5
Database provider: Microsoft.EntityFrameworkCore.InMemory and Microsoft.EntityFrameworkCore.Sqlite (both v5.0.5) - would ideally be using this against NpgSql, but for the demo solution using inmemory.
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.9.5
The text was updated successfully, but these errors were encountered:
I don't think you can do what you're wanting. However, you might consider creating extension methods like IsPlanned or IsOccurring that determine whether an instance matches what you want.
Alternatively, it looks like you're basically just storing an enum in the database. In that case, why not just store it as an enum?
This question is about what or whether there is a good way of telling the context to use only a particular instance of an immutable entity in the context, as it is producing an EF Core instance of entity type cannot be tracked error, I know there are a number of issues pertaining to this, but havn't seen a good solution on how to avoid this when using statically defined entities.
(I suspect this relates to this issue: #20124)
I have these two entities (using vkhorikovs Entity class):
and
From reading and my (limited) understanding what is happening is some query pulls the static defined variable into the context (say
AppointmentStatus.Finished
in this case), such as when getting all the Appointments, so this become the instance known to the context, but when updating an appointment toAppointmentStatus.Finished
from the static reference inAppointmentStatus
, this becomes a second instance tracked by the context. EF then sees these as two separate instances for the same thing (even though they are the same, and immutable), as EF determines equivalency by reference equals.This is the solution with tests to show this in action: https://github.com/robertlarkins/efcore-cannot-track-entity-issue
What would be the best way of telling the context to only use one of the instances and ignore the other?
EF Core version: 5.0.5
Database provider: Microsoft.EntityFrameworkCore.InMemory and Microsoft.EntityFrameworkCore.Sqlite (both v5.0.5) - would ideally be using this against NpgSql, but for the demo solution using inmemory.
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.9.5
The text was updated successfully, but these errors were encountered: