question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
TimKrascommented, Feb 20, 2023

Confirmed fixed in 7.0.3

1reaction
ErikEJcommented, Oct 25, 2022

I have filed a bug in the EF Core repo: https://github.com/dotnet/efcore/issues/29418

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core 7 Rev eng: Missing HasForeignKey when ...
EF Core 7 Rev eng: Missing HasForeignKey when referencing a navigation property with a unique constraint with DataAnnotations on #29418.
Read more >
EF Core: Navigational properties to views
I try to get navigational properties to work between a object that I own and maintain and a view that is controlled by...
Read more >
Breaking changes in EF Core 7.0 (EF7)
Complete list of breaking changes introduced in Entity Framework Core 7.0 (EF7)
Read more >
Entity Framework Core: Foreign key linked with a non-primary ...
The unique constraint means that values cannot be repeated in different entries in a column. Once we configure a field as PrincipalKey, the ......
Read more >
Entity Framework Core Migrations
The migrations feature in Entity Framework Core enables you to make changes to your model and then propagate those changes to your database ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found