-
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
EF Core 6 and newer scaffolding skips foreign keys on tables with multiple references #31169
Comments
@skuami I do not understand you design. Should the foreign keys not be in the Hazmat table and point to Translation? (so that Hazmat.TranslationKeyName and Hazmat.TranslationKeyDescription becomes foreign keys) |
@ErikEJ You're right - it is not a obvious design nor best practice. It is a fairly large application with a database of about 150 tables and one can say it has historicaly grown. Here are some example records and how the translations are "patched" together:
Since the PK of the translation table is a combination of two columns - it is not possible to create a FK from the Hazmat table or all other tables in our database. That's why it is the other way around. Eitherway EF 5 could handle this design and generated following code;
|
@skuami got it and agree there is a bug. I am working on a fix. |
@ajcvickers Patch 6 ?? Or not worth it? |
Thanks Erik for your help! |
@ErikEJ Not for now. We may reconsider with more reports. |
That's unfortunate - that means, we will be unuable to migrate our application from EF 5 to a supported version. |
@skuami Maybe I can fix this in EF Core Power Tools - if you are interested you could sponsor that. |
@ErikEJ - I will look into the EF Core Power Tools this afternoon. I will let you know. |
@ErikEJ - the EF Core Power Tools work like a charm. It solves also an other problem with subfolders and namespaces for schemas. If you could fix the current bug there, that would be awesome. |
@skuami I have created ErikEJ/EFCorePowerTools#1861 |
@skuami |
@ErikEJ |
@skuami Happy to unblock you and thanks for the sponsorship! |
File a bug
The EF Core scaffolder skips foreign keys on tables with multiple references to the same table but on different columns.
This issue occures on following example:
In the database is one table for all translations and other tables refernces translations with foreign key. Tables with only one translated column work just fine. But tables with multiple translated columns (multiple references to translation table) only the first column gets a navigation propery.
This scenario worked fine with EF 5. I think there is an issue in the dupplicate foreign key check. According to the unit test the skip should only happen when the same column has multiple reference to the same table. In my case there are different columns.
The same behaviour can be reproduced with EF 7 and EF 8-preview.
Detailed description of the problem with generated code examples on Stack Overflow:
https://stackoverflow.com/questions/76592136/ef-core-6-scaffolding-skips-foreign-key-on-tables-with-multiple-references
Reproduce the issue
dotnet ef dbcontext scaffold "Server=xxx;Database=xxx;Trusted_Connection=True;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer --no-onconfiguring -f
Include stack traces
The EF Core 6 scaffolder gives following warning and generates only one navigation property eventhough the references are on two different columns:
Include verbose output
Include provider and version information
EF Core version: Tested on 6.0.19, 7.0.8 and 8.0.0-preview.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 11 Version 22H2
IDE: Visual Studio 2022 17.4.5
The text was updated successfully, but these errors were encountered: