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