efcore unique constraint code example

Example 1: nullable unique constraint ef

// Add unique nullable index 
string indexName = "IX_UQ_UniqueColumn";
string tableName = "dbo.ExampleClasses";
string columnName = "UniqueColumn";

Sql(string.Format(@"
    CREATE UNIQUE NONCLUSTERED INDEX {0}
    ON {1}({2}) 
    WHERE {2} IS NOT NULL;",
    indexName, tableName, columnName));

Example 2: how to drop a unique constraint in sql

SHOW CREATE TABLE [tableName]
-- Find the constraintID after CONSTRAINT 'constraintID'
ALTER TABLE [tableName] DROP FOREIGN KEY constraintID