If-(Not-)Exists checks in migrations (SQL Server)

This feature is using SQL Server capabilities.

Required Nuget Package:
Thinktecture.EntityFrameworkCore.SqlServer

Usage

Register ThinktectureSqlServerMigrationsSqlGenerator with EF.

var services = new ServiceCollection()
   .AddDbContext<DemoDbContext>(builder => builder
                                           ...
                                           .UseSqlServer("conn-string", sqlOptions =>
                                                                                    {
                                                                                       sqlOptions.UseThinktectureSqlServerMigrationsSqlGenerator();
                                                                                    })

Use IfExists or IfNotExists to add checks to operations.

migrationBuilder.CreateIndex("IX_OrderItems_ProductId", "OrderItems", "ProductId").IfNotExists();

Supported operations

  • CreateTable - IfNotExists
  • DropTable - IfExists
  • AddColumn - IfNotExists
  • DropColumn - IfExists
  • CreateIndex - IfNotExists
  • DropIndex - IfExists
  • AddUniqueConstraint - IfNotExists
  • DropUniqueConstraint - IfExists

Remarks

The implementation of IMigrationsSqlGenerator will be replaced by ThinktectureSqlServerMigrationsSqlGenerator (derives from SqlServerMigrationsSqlGenerator)