Repository navigation
Description
2 simple entities:
public class Company
{
public int CompanyID { get; set; }
[StringLength(50)]
public string Name { get; set; }
[StringLength(50)]
public string City { get; set; }
[StringLength(50)]
public string Street { get; set; }
[StringLength(10)]
public string Postcode { get; set; }
[StringLength(50)]
public string Country { get; set; }
public List<Management> ManagementSet { get; set; } = new();
}
public class Management
{
public int ID { get; set; }
public string Name { get; set; }
public string Role { get; set; }
public Company Company { get; set; }
public int CompanyId { get; set; }
}Context Class with Entity Splitting for Company:
public class CompanyContext : DbContext
{
public DbSet<Company> Company { get; set; }
public DbSet<Management> Management { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
string connstring = @$"Server=YourServer;Database=EFC_MappingScenarios_EntitySplitting;Trusted_Connection=True;MultipleActiveResultSets=True;encrypt =false";
builder.UseSqlServer(connstring); //.LogTo(CUI.PrintAlternatingColor);
builder.EnableSensitiveDataLogging(true);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region Entity Splitting
modelBuilder.Entity<Company>(
entityBuilder =>
{
entityBuilder
.SplitToTable(
"Address",
tableBuilder =>
{
tableBuilder.Property(x => x.City);
tableBuilder.Property(x => x.Street).HasColumnName("Streetname");
tableBuilder.Property(x => x.Postcode);
tableBuilder.Property(x => x.Country);
});
});
#endregion
}
}This will create a Foreign Key in Management that points to table Address instead of table Company :-(
ALTER TABLE [dbo].[Management] WITH CHECK ADD CONSTRAINT [FK_Management_Address_CompanyId] FOREIGN KEY([CompanyId])
REFERENCES [dbo].[Address] ([CompanyID])
ON DELETE CASCADE
GOI tried to add this:
modelBuilder.Entity<Management>(entity =>
{
entity.HasOne(d => d.Company).WithMany(p => p.ManagementSet)
.HasForeignKey(d => d.CompanyId);
});Makes no difference :-(
provider and version information
EF Core version: 7.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows
Reacted by Oleg and Mathias Schaemelhout
Reacted by Oleg and Copilot
Activity
ajcvickers commentedon Dec 22, 2022
Looks like a bug. /cc @AndriySvyryd
AndriySvyryd commentedon Jan 4, 2023
A workaround is to edit the generated migration file manually.
YoniSL commentedon Nov 4, 2025
Ran into a similar situation, same bug. The suggested workaround did work, even when adding new table splits and properties afterwards.
Would it make sense to have a "HasPrincipalTable" on the wrongly mapped FKs, or would a fix be possible without this additional information
(still prevalent in dotnet ef 9.0.10 / linux and windows)
Fix FK to entity-split entity pointing to wrong (fragment) table
Fix FK to entity-split principal referencing wrong (fragment) table (d…