Skip to content

Validate that filters are the same for shared indexes #24968

Closed

Description

When using base class, attempting to be unique within the specific typed class does not apply the unique with Code-First.

Example excerpt:

public class TestContext : DbContext
{
  DbSet<ImplementClassOne> Ones { get; set; }
  DbSet<ImplementClassTwo> Twos { get; set; }

  protected void OnModelCreating(ModelBuilder builder)
  {
    modelBuilder.Entity<Base>().HasDiscriminator<string>("Type")
      .HasValue<ImplementClassOne>("one")
      .HasValue<ImplementClassTwo>("two");

    modelBuilder.Entity<Base>().Property(base => base.UniqueVal)
      .IsRequired();

    modelBuilder.Entity<Base>().Property(base => base.RequiredVal)
      .IsRequired();

    modelBuilder.Entity<ImplementClassOne>().HasIndex(one => one.UniqueVal)
      .IsUnique()
      .HasFilter("Type = 'one'");

    modelBuilder.Entity<ImplementClassTwo>().HasIndex(two => two.UniqueVal)
      .IsUnique()
      .HasFilter("Type = 'two'");
  }
}

This configuration creates the following Up() method in the Migration file, using Code-First:

protected override void Up(MigrationBuilder migrationBuilder)
{
  migrationBuilder.CreateTable(
    name: "Base",
    columns: table => new
    {
      Id = table.Column<int>(type: "int", nullable: false)
        .Annotation(...),
      UniqueVal = table.Column<string>(type: "nvarchar(max)", nullable: false,
      RequiredVal = table.Column<string>(type: "nvarchar(max)", nullable: false
    },
    constraints: table =>
    {
      table.PrimaryKey("PK_Base", x => x.Id);
    });

  migrationBuilder.CreateIndex(
    name: "IX_Base_UniqueVal_One",
    table: "Base",
    column: "UniqueVal",
    unique: true,
    filter: "Type = 'one'");
}

The second unique filtered index is missing. I can manually insert the index into the migration file. However, I am unable to test the second constraint when using normal unit testing methods, since unit tests normally do not use migrations.

Expected behavior would be for all unique filtered indexes be applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions