-
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
Dissalow defining FKs on properties that are part of an inherited key #2837
Comments
@AndriySvyryd do we automatically wire up |
Triage: We agree this sounds reasonable |
@AndriySvyryd - This would require changes only in |
@smitpatel We should throw in |
How does this issue relate to EF Core's current codebase? On the one hand, when feeding in my large domain model, I get the exception, "The property 'ID' cannot be part of a foreign key on 'T' because it is contained in a key defined on a base entity type." On the other hand, while trying to understand what this exception means and how to fix it, I tested this issue's original comment's code. I expected it to fail with said exception, but it doesn't. The data context contains these members:
And the model consists of these classes:
And it works! That's weird since I'm still encountering the aforementioned exception when I feed in my more complex model. |
@HappyNomad That's not the code described in the first post. You need to configure it like this to get the exception: protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Burger>()
.HasOne(p => p.Bun)
.WithOne(i => i.Burger)
.HasForeignKey<Bun>(b => b.Id);
} |
@AndriySvyryd Thank you for the correction. Sorry, but I still don't understand why this exception is necessary and how to avoid it. As you see in #6792, I have a model in which this exception is an issue. Although, there's a strange workaround in that small example (but not in my real domain model) that involves renaming a property. Is the exception necessary due to either (a) a limitation of TPH mapping in general, or (b) the current EF core implementation specifically? I do not know EF Core's implementation, but I am generally familiar with relational databases and TPH mapping. I do not understand the issue regarding value generation. Please explain the problem more completely, and further illustrate the scenario you are trying to avoid by throwing the exception. |
Consider this model:
There's an FK from
Bun.Id
toBurger.Id
, however there are no FKs defined fromIngredient
. ShouldIngredient.Id
use its own value generator? Should it use value generator fromBurger.Id
? What if there's another type derived fromIngredient
and it has an FK fromId
to a different property? Should we prohibit defining FKs on properties that are part of an inherited key?.The text was updated successfully, but these errors were encountered: