-
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
Scaffold tool can create an inverse property with a similar name to a column property and get confused #25292
Comments
@conficient Can you please post the SQL to create the database tables involved so we can try to reproduce this. |
I've created a repo with the SQL and the generated code for you: https://github.com/conficient/EFscaffoldBug Trimmed out all but the two tables and the relevant columns and the bug is still evident. |
This is model building bug. efcore/src/EFCore/Metadata/Conventions/InversePropertyAttributeConvention.cs Lines 80 to 81 in 9fa54fe
Not sure why that line is comparing property names ignoring case. |
Been like that since the start: 81b1be1#diff-4c3db0c4694e7ac035dd26ec3393ed3e7bd9fd10225d4c3b50129fcaa1dda962R40 Probably to match EF6 behavior. |
@dotnet/efteam What is the suggested fix here? Should we make C# properties in generated entity type unique ignoring casing? |
Note from triage: fix by doing a case-sensitive match first, followed by a case-insensitive match, in the convention. |
Before falling back to case insensitive search Resolves #25292
Before falling back to case insensitive search Resolves #25292
If a table has a foreign key column and a value column with a similar name, then then scaffold tool can create a model which confuses the model builder as they have the same property name if case isn't matched.
Detail
I have a database with a
CPSorder
table with a columnCPSChargePartner
column (smallmoney, nullable
).This table also has a foreign key to a
Partners
table with the foreign key column calledCPSchargePartnerID
.In earlier versions of the
dotnet ef dbcontext scaffold
command had no issues. Recently I upgraded my code to use EF Core tools 5.0.8 and rebuilt the context (full command below):This generated everything without issues. However when I tried to use the context it crashed with a
NullReferenceException
somewhere in the create model process:Cue a lot of headscratching and also referring to this issue. I then tried updating to EF Core 6 preview and trying to run the generated context in .NET 6 preview: this resulted in this:
Cause
So the inverse property name should be
CPSchargePartner
butCPSChargePartner
(the column) was being matched.When the
scaffold
tool ran, it created the following inCPSOrder.cs
:In the related table
Partners.cs
we have:and in the context:
So although everything looks okay, when the model is being created it tries to use the property
CPSChargePartner
which isn't a foreign key and so wasn't in the map. If the mapping/lookup isn't case sensitive perhaps the scaffold tool should refuse to create possible conflicting properties?I was able to work around this by renaming the property so that it would not get picked up:
The text was updated successfully, but these errors were encountered: