5

Is it possible to create a case insensitive unique index in Entity Framework core using Fluent API?

For example, a case insensitive unique index can be defined in Oracle using the following SQL:

create unique index test on "Person"(lower("Name"));

However, as far as I'm aware the only option in EF Core is a case sensitive unique constraint, such as:

builder.HasIndex(e => e.Name)
            .IsUnique();

I have tried the following but it doesn't work:

builder.HasIndex(e => e.Name.ToLower())
            .IsUnique();
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
mozziemoo
  • 51
  • 3

1 Answers1

0

From EF Core 5.0, you can use collations.

builder.Property(e => e.name).UseCollation("SQL_Latin1_General_CP1_CI_AS");
builder.HasIndex(e => e.name).IsUnique();

For oracle, you might want to try a different collation name, like BINARY_CI.

Degravef
  • 120
  • 8