FluentMigrator NonClustered PrimaryKey SqlServer?

I will start working with FluentMigrator today and, unfortunately, I'm sure I know the answer to my question, but is there a good way to get FluentMigrator to create a non-cluster primary key when using the Sql server provider?

I was hoping for something similar. (which obviously does not exist).

  Create.Table("User")
        .WithColumn("Id").AsInt32().Identity().PrimaryKey().WithOptions().NonClustered()

The best option I've found so far is to use Execute with an SQL block, which is much smaller than perfect ... any better options?

+3
source share
3 answers

Unfortunately, the only way to do this was to use:

Execute.Script(path);

, FluentMigrator SQL-, . , script , - . , , , . Fluent , .., , , ( ).

0

, . , script.

public static class FluentMigratorExtensions
{
    public static void CreateNonClusteredPrimaryKey(this Migration migration, string indexName, string columnName, string tableName, string schemaName = "dbo")
    {
        var sql =
            @"ALTER TABLE [{3}].[{2}] ADD CONSTRAINT {0} PRIMARY KEY NONCLUSTERED ({1});";
        sql = string.Format(sql, indexName, columnName, tableName, schemaName);
        migration.Execute.Sql(sql);
    }
}

,

    this.CreateNonClusteredPrimaryKey("PK_Users", 
                                      "Id", 
                                      "Users", 
                                      "security");
0

Source: https://habr.com/ru/post/1793991/


All Articles