ApplyUniqueIndex
Adds a unique index, which is how a value is kept unique in the database rather than only in the code that writes it. Use ApplyIndex for an index that only serves queries.
Usage
using AlmightyShogun.EntityFrameworkCore.ModelBuilding;
modelBuilder.ApplyUniqueIndex<Account>(account => account.Email);using AlmightyShogun.EntityFrameworkCore.ModelBuilding;
modelBuilder.ApplyUniqueIndex<Membership>(membership => new
{
membership.AccountId,
membership.TeamId
});using AlmightyShogun.EntityFrameworkCore.ModelBuilding;
modelBuilder.ApplyUniqueIndex<Account>(
account => account.Slug,
filter: "[Slug] IS NOT NULL"
);WARNING
filter is raw SQL and its identifier quoting is provider-specific: [Slug] on SQL Server, "Slug" on PostgreSQL and SQLite, `Slug` on MySQL and MariaDB.
Parameters
index: Expression<Func<TEntity, object?>>
The property to index, or an anonymous object of properties for a composite index. A composite unique index constrains the combination, not each column on its own.
filter: string?
A provider-specific predicate limiting which rows the constraint covers. Worth setting over a nullable column on SQL Server, which treats two nulls as equal and refuses the second row without it. PostgreSQL, SQLite, MySQL and MariaDB allow repeated nulls.
Default: null
Returns
TheModelBuilder instance with the unique index configured.Type signature
public ModelBuilder ApplyUniqueIndex<TEntity>(
Expression<Func<TEntity, object?>> index,
string? filter = null
) where TEntity : class;