Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FluentMigrator非群集主密钥SqlServer?_C#_.net_Sql Server - Fatal编程技术网

C# FluentMigrator非群集主密钥SqlServer?

C# FluentMigrator非群集主密钥SqlServer?,c#,.net,sql-server,C#,.net,Sql Server,从今天开始使用FluentMigrator,不幸的是,我很肯定我知道我的问题的答案,但是在使用Sql Server提供程序时,有没有什么好办法让FluentMigrator创建非聚集主键 我希望得到类似的东西。(这显然是不存在的) 到目前为止,我发现最好的选择是使用Execute和一个远远不理想的SQL块。。。有更好的选择吗?不幸的是,唯一的做法是使用: Execute.Script(path); 基本上,FluentMigrator已经成为运行SQL脚本的工具,但是fluent接口被忽略了。

从今天开始使用FluentMigrator,不幸的是,我很肯定我知道我的问题的答案,但是在使用Sql Server提供程序时,有没有什么好办法让FluentMigrator创建非聚集主键

我希望得到类似的东西。(这显然是不存在的)


到目前为止,我发现最好的选择是使用Execute和一个远远不理想的SQL块。。。有更好的选择吗?

不幸的是,唯一的做法是使用:

Execute.Script(path);

基本上,FluentMigrator已经成为运行SQL脚本的工具,但是fluent接口被忽略了。基本上,在这一点上,它是我的项目中最重要的脚本运行程序。也许我们可以自己开发,但迁移组件可以按预期工作。流畅的界面太过拘束,在定义表等时通常需要特定于数据库的功能来获得最佳性能(不依赖数据库不是一件好事)。

这并不漂亮,但对我来说很管用。添加一个扩展来包装脚本

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");

为了以防万一,现在可以使用Fluent.Runner.Extensions和SQL Server特定的扩展来解决这个问题


要创建非聚集索引,应使用所述的FluentMigrator.SqlServer

代码如下:

Create.Table("User").WithColumn("Id").AsInt32().Identity();
// than create PK
Create.PrimaryKey("PK_User").OnTable("User").Column("Id").NonClustered();
Create.Table("User").WithColumn("Id").AsInt32().Identity();
// than create PK
Create.PrimaryKey("PK_User").OnTable("User").Column("Id").NonClustered();