Create Unique constraint for 'true' only in EF Core Create Unique constraint for 'true' only in EF Core sql-server sql-server

Create Unique constraint for 'true' only in EF Core


You can specify index filter using the HasFilter fluent API.

Unfortunately it's not database agnostic, so you have to use the target database SQL syntax and actual table column names.

For Sql Server it would be something like this:

.HasIndex(e => new { e.RecordId, e.IsPrimary }).IsUnique().HasFilter("[IsPrimary] = 1");

or

    .HasIndex(e => new { e.RecordId, e.IsPrimary })    .IsUnique()    .HasFilter($"[{nameof(RecordAttachment.IsPrimary)}] = 1");

For more information, see Relational Database Modeling - Indexes documentation topic.