efcore icon indicating copy to clipboard operation
efcore copied to clipboard

duplicate index created when PK is also a FK

Open lcalabrese opened this issue 10 years ago • 1 comments

Steps to reproduce

Consider the following many-to-many definition, with an additional property in the MTM table so we need the intermediate entity:

public class Foo
{
    public int Id { get; set; }        
    public ICollection<FooBar> FooBars { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public ICollection<FooBar> FooBars { get; set; }
}

public class FooBar
{
    public int FooId { get; set; }
    public int BarId { get; set; }
    public string AdditionalProperty { get; set; }
}

OnModelCreating contains:

        builder.Entity<Foo>(b =>
        {
            b.HasKey(x => x.Id);
            b.HasMany(x => x.FooBars).WithOne().HasForeignKey(x => x.FooId).IsRequired();
        });

        builder.Entity<Bar>(b =>
        {
            b.HasKey(x => x.Id);
            b.HasMany(x => x.FooBars).WithOne().HasForeignKey(x => x.BarId).IsRequired();
        });

        builder.Entity<FooBar>(b =>
        {
            b.HasKey(x => new { x.FooId, x.BarId });
        });

The issue

One would expect the primary key of FooBars to be FooId, BarId, and one more index is created due to the FK for BarId. The foreign key FooId REFERENCES Foos(Id) should not create an additional index, because it is already covered by the PK.

However, the generated migration code is:

        migrationBuilder.CreateTable(
            name: "Bars",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Bars", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "Foos",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Foos", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "FooBars",
            columns: table => new
            {
                FooId = table.Column<int>(nullable: false),
                BarId = table.Column<int>(nullable: false),
                AdditionalProperty = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_FooBars", x => new { x.FooId, x.BarId });
                table.ForeignKey(
                    name: "FK_FooBars_Bars_BarId",
                    column: x => x.BarId,
                    principalTable: "Bars",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
                table.ForeignKey(
                    name: "FK_FooBars_Foos_FooId",
                    column: x => x.FooId,
                    principalTable: "Foos",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateIndex(
            name: "IX_FooBars_BarId",
            table: "FooBars",
            column: "BarId");

        migrationBuilder.CreateIndex(
            name: "IX_FooBars_FooId",
            table: "FooBars",
            column: "FooId");

Note the index IX_FooBars_FooId which is being created.

Further technical details

EF Core version: 1.0.0-rc2-final Operating system: Win 7 x64 Visual Studio version: VS 2015 Update 2

lcalabrese avatar Jun 06 '16 06:06 lcalabrese

@AndriySvyryd someone said in triage he believed this to be a dupe. I haven't been able to find any dupe among open bugs, but feel free to check if you can find one.

divega avatar Aug 08 '16 18:08 divega