dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.63k stars 3.15k forks source link

An invalid index is being created #34358

Closed NikVladi closed 4 weeks ago

NikVladi commented 1 month ago

Microsoft.EntityFrameworkCore 8.0.7. Npgsql.EntityFrameworkCore.PostgreSQL 8.0.4.

I have two models.

using Microsoft.EntityFrameworkCore;

Country: public class Country { public int Id { get; set; } public string Name { get; set; }
}

Region: public class Region {
public int Id { get; set; } public string Name { get; set; } public Country Country { get; set; }
}

The issue: //Correctly. An index is created by Name and CountryId as expected [Index("Name", "CountryId", IsUnique = true)] public class Region ...

//Incorrectly. An index is created by Name and Id as unexpected [Index(nameof(Name), nameof(Country.Id), IsUnique = true)] public class Region ...

roji commented 1 month ago

//Incorrectly. An index is created by Name and Id as unexpected [Index(nameof(Name), nameof(Country.Id), IsUnique = true)]

What's unexpected about this? In C#, nameof(Country.Id) returns "Id" - this has nothing to do with EF Core. You can add a CountryId property to Region if you want - EF will automatically use that as the foreign key, and then you can reference that in your nameof.

ajcvickers commented 4 weeks ago

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.