-
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
Using ForeignKeyAttribute to control the name of a FK property unexpectedly swaps principal and dependent sides and sets cardinality to 1:1 #4909
Comments
I'm wondering if the data annotations shouldn't have exactly the same semantics that they did in EF6? Isn't this inherit to their nature as independent from any given technology? If ForeginKey doesn't have a single set of semantics, then how do other technologies that want to consume the annotation decide what it means? |
@ajcvickers Are you saying the current behavior is the desired one or not? |
Actually for this particular scenario in EF6 you would get the following exception: "An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll Additional information: The ForeignKeyAttribute on property 'Author' on type 'ConsoleApplication43.Post' is not valid. The foreign key name 'UserId' was not found on the dependent type 'ConsoleApplication43.Post'. The Name value should be a comma separated list of foreign key property names." Note that FKs in shadow state were not supported. Regardless I do agree with @ajcvickers that backwards compatibility (or maintaining consistent semantics of how data annotations are interpreted) is important. Although I believe if there is a case in which the old behavior was very broken we should seriously consider changing it in the new stack. In particular I don't believe there are many things out there that try to reason about |
Actually there might be things out there (other than EF6) that do reason about |
@AndriySvyryd It was more a general comment. There have been a few issues filed recently about the way data annotations work and I was saying maybe in general we should be making sure we don't change the semantics. @divega If you make the relationship explicitly 1:1 (add another nav property) does it still throw with the old stack? |
If you add the navigation property on the other side you get this: "An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll Additional information: Unable to determine the principal end of an association between the types 'ConsoleApplication43.User' and 'ConsoleApplication43.Post'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations." If you configure the relationship explicit I believe the |
I introduced
[ForeignKey("UserId")]
in a model like the one below with the sole purpose of affecting the name of the foreign key forPost.Author
.Instead EF Core went off to find that "UserId" matched the name of the principal key on
User
, and so decided to create a 1:1 identifying relationship betweenPost.PostId
andUser.UserId
.This is probably by design right now but I believe it is very confusing. In fact I find it strange that using
ForeignKeyAttribute
on a reference navigation property would turn a relationship around and make the reference point from principal to dependent.I only realized that something was wrong when the navigation property started to be populated with completely arbitrary entities.
Additional repro code:
The text was updated successfully, but these errors were encountered: