-
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
Model configuration: FK exception with some combinations of property names and inheritance #6792
Comments
I've updated my original comment with a model that reproduces the issue in isolation. It was difficult to whittle down this small model since reproducing the issue depends on class and property names. |
It shouldn't depend on the naming as you've described, so there could be a bug in this particular scenario, it should still throw if modelbuilder.Entity<T>().Property<int>("PId");
modelbuilder.Entity<T>().HasOne(t => t.P).WithOne().HasForeignKey<T>("PId"); |
Okay, so now I understand the default behavior is as stated here: "In a one-to-one relationship, the primary key acts additionally as a foreign key and there is no separate foreign key column for either table.". But then why not do the correction automatically similar to how this happens automatically, instead of throwing the exception and requiring extra code? Anyway, please explain why the exact same exception is thrown even for this model, where T only participates in a one-to-many relationship. Is it a bug too?
This example has a similar strange workaround: flip the L1 and L2 names and then the exception disappears. |
Add more details to the exception message. Fixes #6792
Add more details to the exception message. Fixes #6792
Fixed in 66c4c79 |
Thanks for the fix here and on #6814. I see both are on the "feature/1.2.0" branch. I'm now using MyGet's ASP.NET Core "dev" branch feed with this version configured:
So I can't benefit from your changes until after the 1.1.0 release, at which time I'll be able to indicate the version as 1.2.0-preview1-*, right? I'd just like to verify that I correctly understand, so I don't wait unnecessarily. I looked at the "aspnetcore-feature-work" feed but don't see EF-Core (not sure I should use that feed even if it were there, though). |
@HappyNomad Correct. Hopefully you won't need to wait long. |
@pranavkm @Eilon any idea why the build output of our feature/1.2.0 branch is not landing in the aspnetcore-feature-work feed? I was under the impression it was automatic 😄 |
@divega unfortunatelty it's not automagic. You have to tell the build to push the packages. Here's what Razor does to push - https://github.com/aspnet/Razor/blob/feature/razor-pages/makefile.shade. |
You are free to merge 1.2.0 changes into dev - the branch is open. |
@AndriySvyryd To clarify, did you implement this suggestion? I'm using version Also to clarify, you suggested the workaround:
But I believe it should instead be:
Right? |
@HappyNomad Yes, the fix is to choose FK properties that are compatible with the base type if you haven't specified any explicitly, so no extra configuration would be needed. |
Both workarounds are valid, it depends on what type you want to be the dependent one. |
@AndriySvyryd I looked at the generated model and it's reversing the principle/dependent roles from what I need. I see this issue with other one-to-one relationships in my model, too. My model classes lack navigation properties that point from dependent to principle. I would like the model builder to leverage my consistent approach to navigation to correctly determine principle/dependent roles. The documentation says, "EF will choose one of the entities to be the dependent based on its ability to detect a foreign key property." In the absence of a FK property, please augment this behavior such that, when only one end of a relationship has a navigation property, then that property is assumed to be on the principle entity pointing to the dependent. This won't always be true for everybody, but I think it will be true more often than the reverse. If someone strongly disagrees, then at least provide a Or is there at least some attribute I can add to my model classes' properties, so that the model builder generates as I need, without the extra configuration in For the time being at least, I'll define my own |
@HappyNomad We actually tried to use the proposed heuristic in addition to others before the 1.0.0 release, but it became harder to explain and understand how EF chooses the dependent side, while it didn't improve the success rate that much, as many users have different expectations. |
I'm mapping a complex domain model and encountered 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." Looks like the throwing of this exception was introduced in #2837.
At first I thought this was due to either a limitation of TPH mapping in general, or the current EF core implementation specifically. But now I think it's a bug in EF Core. It was difficult to reproduce in isolation since it obscurely depends on property names, too.
Trying to create a database using this context and model will reproduce the issue:
A workaround for this example is to rename the context's "Ls" DbSet property to "Zs". Unfortunately, I've already come across a case in my real domain model where this simple hack doesn't help. That is, it removes the exception for one class but that, in turn, causes the same exception to occur for another class. Removing one of the properties but not the other doesn't remove the exception, either.
The text was updated successfully, but these errors were encountered: