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
Hi, I found an issue when I try to run this code:
var place = await context.Places.Include(x => x.Organization).FirstOrDefaultAsync(x => x.Id == id);
InvalidCastException: Unable to cast object of type 'PlaceManagement.Place' to type 'OrganizationManagement.Organization'.
if I use AsNoTracking works fine:
await context.Places.Include(x => x.Organization).AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
It seems to be a bug because I have a specific scenario designed to simplify record retrieval. Here's the context: I have two classes, Organization and Place, both inheriting from EntityBase. The EntityBase class includes an ID property, which is a GUID.
The database already contains Organization records, and Place is a new feature. Place and Organization have a one-to-one relationship, and Place can also be associated with Location (another entity). In this setup, the Place ID shares the same GUID as the associated Organization or Location.
Based on this structure, when I attempt to retrieve an Organization, an error is thrown.
Hi, I found an issue when I try to run this code:
var place = await context.Places.Include(x => x.Organization).FirstOrDefaultAsync(x => x.Id == id);
InvalidCastException: Unable to cast object of type 'PlaceManagement.Place' to type 'OrganizationManagement.Organization'.
if I use AsNoTracking works fine:
await context.Places.Include(x => x.Organization).AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
It seems to be a bug because I have a specific scenario designed to simplify record retrieval. Here's the context: I have two classes, Organization and Place, both inheriting from EntityBase. The EntityBase class includes an ID property, which is a GUID.
The database already contains Organization records, and Place is a new feature. Place and Organization have a one-to-one relationship, and Place can also be associated with Location (another entity). In this setup, the Place ID shares the same GUID as the associated Organization or Location.
Based on this structure, when I attempt to retrieve an Organization, an error is thrown.
Project: InvalidCast-EF.zip
Git Project: https://github.com/wbaldanw/ef-invalid-operation-exception
The text was updated successfully, but these errors were encountered: