-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EF Core instance of entity type cannot be tracked #10
Comments
As a side note, this issue can be avoided by getting the desired AppointmentStatus instance from the db directly rather than from the static entity value, but this defeats the purpose of having static values, and requires additional calls to the db. |
I finally found EFCore issues that (appear to) relate to this issue: @vkhorikov What is your thoughts on the best way of resolving this issue when using entities with static values (immutable entities)? Is there anything that could be added to the Entity class (in CSharpFunctionalExtensions) to assist with this? I have also found the following, but unsure if they will help for the above situation:
|
Not sure why this is not working. Try adding lazy loading to your reference project, maybe that's the difference here. It's impossible to make changes in CSharpFunctionalExtensions to avoid this issue, it is core to how EF keeps track of entities in its DbContext. As usual, it's unnecessarily overcomplicated; it's a solved problem in NHibernate. |
In our main project we have lazy loading, and this issue is occurring 😢. I couldn't figure out if or how to add lazy loading to the example solution, I suspect that the InMemoryDB doesn't cater for it. From reading and my (limited) understanding what is happening is some query pulls the static defined variable into the context (say You're right, because of how EF tracks entities (using reference equals) nothing on the Entity class will help. At this stage I guess we continue with pulling the values from the db when necessary until EF better handles this (whenever that might be). I'll go and create this as an EFCore issue and see what is said. Thanks again for your response @vkhorikov. |
@robertlarkins I have the same issue in latest EF Core. What is the solution to this? How can this be fixed? Thank you |
FYI I opened a ticket for this here |
I've been following the approaches proposed in this repo and the associated pluralsight course, and they have worked really well, but recently been getting the following exception thrown by EFCore:
I have created a minimum example to demonstrate the issue (using EFCore inmemory and a Sqlite DB) here: https://github.com/robertlarkins/efcore-cannot-track-entity-issue
As far as I can determine it is related to static entities (such as Course in this repo, or Appointment Status in my linked repo), and it occurs when the context is holding an instance of AppointmentStatus pulled from the database, and the same AppointmentStatus is added to the context from the static AppointmentStatus reference in code. When trying to save the context to the database the above exception gets thrown.
This exception occurs on this line:
so it never gets to the point of trying to set the EntityState as unchanged.
Is this an issue you have come across before? I couldn't find how this repo stops this occurring, hopefully I have just missed something, but if not, then I assume it could potentially happen within this repo as well. Alternatively should this issue be presented on the EFCore issue tracker?
The text was updated successfully, but these errors were encountered: