Skip to content
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

Missing HasForeignKey when referencing a navigation property with a unique constraint. (EF Core 7 preview) #1554

Closed
TimKras opened this issue Oct 24, 2022 · 6 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@TimKras
Copy link

TimKras commented Oct 24, 2022

Describe what is not working as expected.

Provide steps to reproduce

The tool is generating this code:

          entity.HasOne(d => d.VrijstellingsCodeBHNavigation).WithMany(p => p.Klant)
                .HasPrincipalKey(p => p.VrijstellingsCodeBH)
                .HasConstraintName("FK_Klant_BTWVrijstellingsCode");

At runtime I get this exception:
image
Indeed, the column does not exists in the database.

When adding manual this line, everything is working like it should:

            entity.HasOne(d => d.VrijstellingsCodeBHNavigation).WithMany(p => p.Klant)
                .HasForeignKey(p => p.VrijstellingsCodeBH)
                .HasPrincipalKey(p => p.VrijstellingsCodeBH)
                .HasConstraintName("FK_Klant_BTWVrijstellingsCode");

--> it looks like the HasForeignKey() is missing.

Provide technical details

  • EF Core version in use: EF Core 7

  • Is Handlebars used: no

  • Is T4 used: no

  • Is .dacpac used: no

  • EF Core Power Tools version: latest version

  • Database engine: SQL Server

  • Visual Studio version: Visual Studio 2022

@ErikEJ
Copy link
Owner

ErikEJ commented Oct 24, 2022

What happens when you do not use t4?

I need a full SQL schema to investigate

@TimKras
Copy link
Author

TimKras commented Oct 25, 2022

@ErikEJ
I have created an example project to demonstrate the problem. See attached, there is also a script to generate the database.

It is not related to T4 template usage.
The problem does only occur when using 'EF Core 7 (preview)' in combination with 'Use DataAnnotation attributes to configure the model'.
When unchecking the 'Use DataAnnotation attributes' option, the HasForeignKey statement is generated (and the application is working).
EFTest.zip

@TimKras TimKras changed the title Missing HasForeignKey when referencing a navigation property with a unique constraint. Missing HasForeignKey when referencing a navigation property with a unique constraint. (EF Core 7 preview) Oct 25, 2022
@ErikEJ
Copy link
Owner

ErikEJ commented Oct 25, 2022

Yes, I am able to repro - and EF Core 6 reverse engineering generates this with DataAnnotations enabled:

 entity.HasOne(d => d.ColorCodeNavigation)
                    .WithMany(p => p.Car)
                    .HasPrincipalKey(p => p.ColorCode)
                    .HasForeignKey(d => d.ColorCode)
                    .HasConstraintName("FK_Car_Color");

@ErikEJ
Copy link
Owner

ErikEJ commented Oct 25, 2022

I have filed a bug in the EF Core repo: dotnet/efcore#29418

@ErikEJ ErikEJ added bug Something isn't working wontfix This will not be worked on labels Oct 25, 2022
@ErikEJ ErikEJ closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2022
@TimKras
Copy link
Author

TimKras commented Feb 20, 2023

Confirmed fixed in 7.0.3

@ErikEJ
Copy link
Owner

ErikEJ commented Feb 20, 2023

@TimKras Thanks for the update, EF Core Power Tools uses 7.0.3 now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants