efcore icon indicating copy to clipboard operation
efcore copied to clipboard

InvalidCastException when using Proxies on two different subclasses from the same class with the same id value (TPC strategy). Am I doing something wrong?

Open Joelfo opened this issue 1 year ago • 1 comments

The problem

I have these two classes, Patient and District, sharing the same BaseModel subclass which defines the Id property for both classes. The BaseModel is just being used as a "syntatic sugar" for my models that share similar model properties, so it doesn't have a table and it is mapped to use the TPC mapping strategy... The issue first appeared when retrieving a list of Patients with the ToListAsync() method, the error occurs in the District property from the Patient where the EF Core Proxy seems to be trying to use a PatientProxy for it, instead of a District Proxy.

InvalidCastException: Unable to cast object of type 'Castle.Proxies.PatientProxy' to type 'CaitMazziniApp.Models.Core.District'.

Probable reason and how I fixed it

I noticed that the issue only happens when I have a Patient with an specific Id value and a District, being pointed by a Patient which could also be the former, with an Id of the same value. In my case, I had a Patient with id of 140 and a another Patient pointing to a District with id of 140, and the issue stopped when i made it point to another District. I couldn't stick with this limitation so I had decided to dive deeply into searching for a solution and ended up founding that the problem was related to the BaseModel Class. I found two possible solutions:

  • Altering one of the classes so it uses an interface instead of a superclass and have its own Id property.
  • Stop using proxies.

image

image

image

image

I don't believe it is a urgent error and maybe what I'm using a bad pattern by using a superclass for all of my models, so the main reason i'm posting this issue is because it tooked me a while to discover its reason and it can help someone who have the same problem in the future. Thanks!

Joelfo avatar Feb 19 '24 01:02 Joelfo

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Feb 19 '24 11:02 ajcvickers

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

ajcvickers avatar Mar 06 '24 19:03 ajcvickers

I am having what I believe is a similar issue. I will try to make a minimum example but we only experienced the issue after our entity model grew to a certain size.

celluj34 avatar Mar 13 '24 00:03 celluj34

@Joelfo This problem is like you described. You have two entities with the same ID; since they inherit from the same base class and have the same ID, EF will conflate the two objects when you try to load them into the tracking store. You can either 1) make them not inherit the same base class or 2) ensure all entities have different IDs. We accomplished this by not inserting IDs into the database and letting the sequence handle it for us.

celluj34 avatar Mar 13 '24 13:03 celluj34