dotnet / dotnet/efcore

[DX] Confusing exception message when trying to change primary or alternate key.

Open
#36,481 0 comments 0 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-change-tracking
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Just for the sake of experiment I checked the behavior of EF when alternate key is changed.
As expected, it throws the same exception as when changing the primary key:
> System.InvalidOperationException: 'The property 'Entity.Id' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.'

But I find the second half of the message confusing:
> To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.

There's no relationship in my case. It's just the attempt to change the key. Adding the remark about principal and dependent is relevant only when some parts of the key belong to the FK. I believe EF is capable to check it using metadata and adjust the message.

At least I'd reword the explanation and be more explicit insted of using ` identifying foreign key`. Something like this:
> System.InvalidOperationException: 'The property 'Entity.Id' is part of a key and so cannot be modified or marked as modified. If you are changing the principal, and components of the foreign key pointing to the principal entity belong to the key of the dependent entity, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.