-
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
Entity that owns multiple entities of the same type with navigations back to the owning type makes model construction fail #35346
Comments
you have two UserId columns in your Owned entity. As your User "Owns" a "UserTimestamp" there is an implicit "UserId"-Column in that entity. i imagine just removing the UserId from UserTimestamp would be enough to fix it |
@kirides Yes, that is by design. The |
@alexanderchr what happens if modify it to this? [ForeignKey(nameof(User))]
public int UserId { get; set; }
public User User { get; set; } |
I get the same error with that change. But as you can see from the model with |
Thank you for the fix @AndriySvyryd. |
I have lots of entities for which I need to track when and by whom they were created and last updated. I have solved this by putting an owned type
UserTimestamp
on tracked entities asCreated
andUpdated
. Something like this:This worked well until I needed to add tracking to the
User
model, probably because of the self referencing that this introduces. If I putCreated
andUpdated
as above on theUser
model, EF throws during model construction.Below is a minimal reproduction:
which throws
However, if I remove the
Updated
property from the type and the configuration, the following model is created, which looks as expected:Everything also works if I make a copy of the class
UserTimestamp
and use that copy for theUpdated
property (so thatCreated
andUpdated
have nominally different, but otherwise equivalent, types).It seems to me that since it works to have navigations back to owning entity type for the case when the owning type owns a type once, and also when it owns multiple (nominally different) types, the expected behaviour is that is should also work for the case when it owns the same type multiple times.
Include provider and version information
EF Core version: 9.0.0
Database provider: occurs with both SQLServer and Postgres providers
Target framework: .NET 9.0
Operating system: N/A
IDE: N/A
The text was updated successfully, but these errors were encountered: