Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddMigration doesn't create all indexes when using different filters or names #18159

Closed
cornem opened this issue Oct 1, 2019 · 3 comments
Closed

Comments

@cornem
Copy link

cornem commented Oct 1, 2019

When you define multiple indexes for an entity over the same columns but with a different filter expression and/or name, EF will only create a migration statement for the last one.

Steps to reproduce

Create a project with this model:

public class State
{
    public Guid Id { get; set; }

    public DateTime LastModified { get; set; } = DateTime.UtcNow;

    public int? CustomerId { get; set; }

    public int? ItemId { get; set; }

    public int? ItemGroupId { get; set; }

    [Required, StringLength(450)]
    public string RemoteId { get; set; }
}

public class StateDbContext : DbContext
{
    public DbSet<State> State { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<State>(s =>
        {
            s.HasIndex("RemoteId")
                .HasFilter("CustomerId IS NOT NULL")
                .HasName("IX_State_RemoteId_CustomerId")
                .IsUnique();

            s.HasIndex("RemoteId")
                .HasFilter("ItemId IS NOT NULL")
                .HasName("IX_State_RemoteId_ItemId")
                .IsUnique();

            s.HasIndex("RemoteId")
                .HasFilter("ItemGroupId IS NOT NULL")
                .HasName("IX_State_RemoteId_ItemGroupId")
                .IsUnique();
        });
    }
}
  • Notice that even though the index uses the same property RemoteId, the name and filter for each index is different (I can create such indexes on SQL server with T-SQL)
  • Add a migration: dotnet ef migrations add CreateInitialSchema
  • Notice that the generated migration only contains the index IX_State_RemoteId_ItemGroupId.

Further technical details

EF Core version: both 2.* and 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: netcoreapp2.2 / netcoreapp3.0
Operating system: Windows 10
IDE: N/A

@cornem cornem added the type-bug label Oct 1, 2019
@cornem cornem changed the title Migration should create indexes for same columns but different filters AddMigration doesn't create all indexes when using different filters or names Oct 1, 2019
@bricelam
Copy link
Contributor

bricelam commented Oct 2, 2019

This is a limitation in our Fluent API. s.HasIndex("RemoteId") will get or add the single index covering that property. Subsequent calls to HasFilter and HasName are overriding the configuration for that index.

@AndriySvyryd Can you work around this by dropping down to Metadata? Do we have an issue on our backlog about addressing indexes by name in the fluent API?

@AndriySvyryd
Copy link
Member

There is no way of doing this in Metadata.
Duplicate of #4150

@bricelam
Copy link
Contributor

bricelam commented Oct 2, 2019

Note, you can manually add the indexes by adding calls to migrationBuilder.CreateIndex() inside a migration.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants