-
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 7 Rev eng: Missing HasForeignKey when referencing a navigation property with a unique constraint with DataAnnotations on #29418
Comments
@AndriySvyryd Is it by design that this model creates a new class TestContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>().HasOne(d => d.Color).WithMany(p => p.Cars)
.HasPrincipalKey(p => p.ColorCode);
}
}
class Car
{
public int Id { get; set; }
public string? ColorCode { get; set; }
public virtual Color? Color { get; set; }
}
class Color
{
public int Id { get; set; }
public string ColorCode { get; set; } = null!;
public virtual ICollection<Car> Cars { get; } = new List<Car>();
} |
Yes, we only match these patterns:
|
@AndriySvyryd In that case, why do we create a foreign key name by convention ( |
I just wanted to add that this problem only occurs in EF Core 7 (RC). |
@ajcvickers Because naming it |
Notes from triage: ideally, the rules would not result in this behavior. However, rather than making the rules even more complex we decided that it would be better here for scaffolding to respect the existing rules, as it did in EF Core 6.0.l |
No plan to fix for 7.0?? |
@ErikEJ We are going to consider patching. |
Stranger and stranger... Trying to configure it with an attribute doesn't work. // Still not used. Still get shadow property 'ColorCode1'
public string? ColorCode { get; set; }
[ForeignKey(nameof(ColorCode))]
public virtual Color? Color { get; set; } |
What did 6.0 scaffold? |
Scaffold? It never scaffolded data annotations when the principal key is non-primary. It always used fluent API. But I'm just playing with model building to see which data annotations we can scaffold. I'll just work around the |
Filed #29448 |
…nate key Also adds coverage for more scenarios involving alternate principal keys Fixes dotnet#29418
…nate key Also adds coverage for more scenarios involving alternate principal keys Fixes dotnet#29418
@ajcvickers will this be patched in version 7? If so, is it possible to give an estimation on the timing? |
@TimKras Possibly, although you are the only person to have reported it so far, so right now it might not meet the customer impact bar required for a servicing fix. |
Note from triage: this issue covers a wider range of cases than initially thought; we should patch. |
…nate key Also adds coverage for more scenarios involving alternate principal keys Fixes dotnet#29418
🩹 WorkaroundI may be stating the obvious here, but one workaround is to not use data annotations when scaffolding. Or, you can manually add |
A better temporary workaround is adding the missing
|
Confirmed fixed in 7.0.3 |
File a bug
Using EF Core 7 SQL Server reverse engineering with "Use Data annotations" enabled, the foreignkey configuration is missing.
See description and link to repro schema here: ErikEJ/EFCorePowerTools#1554
@bricelam
EF Core version: 7.0 RC2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6
Operating system: Windows
The text was updated successfully, but these errors were encountered: