We have an internal ASP.NET Core web application for tracking projects, currently using EF Core 6. When I scaffolded the existing SQL Server database, all of the foreign keys were created as navigation properties.
Here is a snippet of two related tables Projects
and ProjectStatus
:
public partial class MainProjectsListing
{
public int Id { get; set; }
public int? ProjectNumber { get; set; }
public int? ProjectStatus { get; set; }
public virtual ProjectStatus ProjectStatusNavigation { get; set; } = null!;
}
public partial class ProjectStatus
{
public int Id { get; set; }
public string? Status { get; set; }
public string? StatusDescription { get; set; }
public virtual ICollection<MainProjectsListing> MainProjectsListing { get; set; }
}
When scaffolding the same database in my .NET 8 web application using EF Core 8, the public virtual projectstatus
is not created in the Projects
entity, and the public virtual ICollection
is not created in the ProjectStatus
entity.
Adding these after the fact throws errors, and is impractical as there are several more tables and columns involved.
Am I doing something wrong, or is this not a feature that was passed from EF Core 6 to EF Core 8? Is there a trick to getting the navigation properties to be created when scaffolding the database?
We have an internal ASP.NET Core web application for tracking projects, currently using EF Core 6. When I scaffolded the existing SQL Server database, all of the foreign keys were created as navigation properties.
Here is a snippet of two related tables Projects
and ProjectStatus
:
public partial class MainProjectsListing
{
public int Id { get; set; }
public int? ProjectNumber { get; set; }
public int? ProjectStatus { get; set; }
public virtual ProjectStatus ProjectStatusNavigation { get; set; } = null!;
}
public partial class ProjectStatus
{
public int Id { get; set; }
public string? Status { get; set; }
public string? StatusDescription { get; set; }
public virtual ICollection<MainProjectsListing> MainProjectsListing { get; set; }
}
When scaffolding the same database in my .NET 8 web application using EF Core 8, the public virtual projectstatus
is not created in the Projects
entity, and the public virtual ICollection
is not created in the ProjectStatus
entity.
Adding these after the fact throws errors, and is impractical as there are several more tables and columns involved.
Am I doing something wrong, or is this not a feature that was passed from EF Core 6 to EF Core 8? Is there a trick to getting the navigation properties to be created when scaffolding the database?
We looked a little deeper after scaffolding another test database, which worked. It turns out that all of the foreign keys in the target database had disappeared. Our Dotnet6 application still worked without these as they were already in the dbContext model.
After re-defining all of the keys everything works as it should.
Thanks to Gert Arnold for suggesting to look at the database schema as we were unaware of the missing keys.
You might want to read this document about breaking changes between EF versions. I only looked at version 8, but there may be other breaking changes in version 7.
Scaffolding may generate different navigation names Tracking Issue #27832
Old behavior
Previously when scaffolding a DbContext and entity types from an existing database, the navigation names for relationships were sometimes derived from a common prefix of multiple foreign key column names.New behavior
Starting with EF Core 8.0, common prefixes of column names from a composite foreign key are no longer used to generate navigation names.