Skip to content

Entity Framework Core generates a Foreign Key to the wrong table when using Entity Splitting #29915

Closed

Description

@HSchwichtenberg

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
GO

I 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

Activity

  1. ajcvickers commented on Dec 22, 2022

    @ajcvickers
    Contributor

    Looks like a bug. /cc @AndriySvyryd

  2. AndriySvyryd commented on Jan 4, 2023

    @AndriySvyryd
    Member

    A workaround is to edit the generated migration file manually.

  3. added this to the Backlog milestone on Jan 5, 2023
  4. added theissue type on Jun 15, 2025
  5. YoniSL commented on Nov 4, 2025

    @YoniSL

    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)

  6. modified the milestones: Backlog, 11.0.0 on Nov 5, 2025
  7. self-assigned this
    on Nov 5, 2025
  8. added a commit that references this issue on Jul 9, 2026
    a3e29d4
  9. added a commit that references this issue on Jul 9, 2026
    9b372a7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions