-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
HasKey() throws confusing exception about redefining key if no inheritance was intended #3863
Comments
@smitpatel feel free to start looking at this. Although it isn't triaged @ajcvickers @AndriySvyryd @rowanmiller and I already discussed it. |
@divega Please check your E-mail - I sended you complete solution with this bug... |
I think this is applicable to this discussion, I have a model with two types:
and
in OnModelCreating I have the following snippet:
this throws the following exception: Unhandled Exception: System.InvalidOperationException: The derived type 'Donor' cannot have keys other than those declared on the root type 'Person'. This is confusing to me, because both entities have the same key. |
Resolve #3863 A common cause of this exception is unintentionally including a base type in the model. Adding info about how to stop this happening to the exception message. **Old Message:** The derived type 'Address' cannot have keys other than those declared on the root type 'AddressBase'. **New Message:** A key cannot be configured on 'Address' because it is a derived type. The key must be configured on the root type 'AddressBase'. If you did not intend for 'AddressBase' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model.
@maumar What was your solution to this? |
@ibrahim-islam The key must be defined on the base type because EF allows assigning derived entities to navigations configured with the base entity type. |
Work item is to add info about how you an get into this case.
From #3727 (comment) by @miroslavsiska: the following throws
InvalidOperationException: The derived type 'CashDesk.Models.AllergenQuantityMaterial' cannot have keys other than those declared on the root type.
Note that this is still a negative scenario, e.g. there cannot be a navigation property of the base type if the base type does not define the key. But the exception isn't very helpful if the type wasn't intended as an entity type in the first place.
Repro code:
The text was updated successfully, but these errors were encountered: