如何使用EF Core的Code First创建列存储索引。

3

我们想要启动一个新项目,决定从一开始就在某些表中使用聚集索引和列存储索引,如何使用Code First EF Core 3.1实现这一目标?

1个回答

0
  • 你应该将主键更改为非聚集索引:

    entity.HasKey(e => e.Id).IsClustered(false);
    
  • 然后在迁移文件夹中添加手动迁移,我添加了一个名为30000000000001_AddMyColumnStoreIndex.cs的文件,并在文件中添加了以下代码

using DDFHandler;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using System.Diagnostics.CodeAnalysis;

namespace MyNameSpace
{
    [DbContext(typeof(MyApplicationContext))]
    [Migration("30000000000001_AddMyColumnStoreIndex")]
    public class V1_AddMyColumnStoreIndex : Migration
    {
        protected override void Up([NotNullAttribute] MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("CREATE CLUSTERED COLUMNSTORE INDEX [cci] ON [sch].[TableName]");
        }
        protected override void Down(MigrationBuilder migrationBuilder)
        {
        }
    }
}


请注意,您应该将[sch].[TableName]替换为您的表名和架构。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接