【问题标题】:Create nonclustered index with Entity Framework Core 5.0.4 with a GUID primary key使用带有 GUID 主键的 Entity Framework Core 5.0.4 创建非聚集索引
【发布时间】:2021-06-16 12:30:58
【问题描述】:

我有这个实体类:

public class StatusCode
{
    public const String ClassName       = nameof(StatusCode);
    [Key]
    public Guid         UniqueID        { get; set; } // Primary Key
    public Char         Level           { get; set; } // R=Root, C=Code Group, I=Item
    public Guid         CodeGroup       { get; set; } // F Key - Which group of codes this code belongs to (self reference to this table)
    public String       Code            { get; set; } // Public short form code, normal public use in tabular reports
    public String       FullName        { get; set; } // Full public name, normal public stand alone display
    public String       Description     { get; set; } // Description displayed to explain this code to the public, typically a full sentance
    public String       PublicRemarks   { get; set; } // Longer public remark about this code, typically a few paragraphs
    public String       PrivateRemarks  { get; set; } // Internal use only remark, for display to administrators about the usage of this code
    public Boolean      AvailableYesNo  { get; set; } // Flag to show if this code is available for usage on new records
    public DateTime     AvailableFrom   { get; set; } // Date time that this code is available for first use
    public DateTime     AvailableTo     { get; set; } // Date time that this code is no longer available for use
}

通过这次迁移:

protected override void Up(MigrationBuilder migrationBuilder)
{
      String lLogHdr = $"{ClassName}.{HCG_Misc.CallingMethodName()} ";
      Console.WriteLine( $"{lLogHdr} - Start" );
      migrationBuilder.CreateTable(
          name: "StatusCode",
          columns: table => new
          {
            UniqueID       = table.Column<Guid    >( type: "uniqueidentifier", nullable: false ),
            Level          = table.Column<string  >( type: "nvarchar(1)"     , nullable: false ),
            CodeGroup      = table.Column<Guid    >( type: "uniqueidentifier", nullable: false ),
            Code           = table.Column<string  >( type: "nvarchar(450)"   , nullable: true  ),
            FullName       = table.Column<string  >( type: "nvarchar(max)"   , nullable: true  ),
            Description    = table.Column<string  >( type: "nvarchar(max)"   , nullable: true  ),
            PublicRemarks  = table.Column<string  >( type: "nvarchar(max)"   , nullable: true  ),
            PrivateRemarks = table.Column<string  >( type: "nvarchar(max)"   , nullable: true  ),
            AvailableYesNo = table.Column<bool    >( type: "bit"             , nullable: false ),
            AvailableFrom  = table.Column<DateTime>( type: "datetime2"       , nullable: false ),
            AvailableTo    = table.Column<DateTime>( type: "datetime2"       , nullable: false )
          },
          constraints: table => { table.PrimaryKey("PK_StatusCode_UniqueID", x => x.UniqueID);
          });

      migrationBuilder.CreateIndex( name: "Index_StatusCode_Code"     , table: "StatusCode", column: "Code"      );
      migrationBuilder.CreateIndex( name: "Index_StatusCode_CodeGroup", table: "StatusCode", column: "CodeGroup" );
      Console.WriteLine( $"{lLogHdr} - Complete" );
}

当我跑步时

PM> Update-Database

我得到了一个带有聚集主键索引的数据库表,这对于 GUID 索引来说简直是疯了。我需要做什么才能获得非聚集索引?

【问题讨论】:

    标签: indexing entity-framework-core primary-key clustered-index non-clustered-index


    【解决方案1】:

    EF Core 中的原则是每个数据库提供程序都添加其特定的配置扩展方法。你没有说你的目标是什么数据库,所以假设是SqlServer,你需要的配置方法叫IsClustered并扩展了关键配置(HasKey)。

    所以你需要添加以下内容

    modelBuilder.Entity<StatusCode>()
        .HasKey(e => e.UniqueID)
        .IsClustered(false);
    

    并重新生成迁移。现在它应该包含类似这样的内容

    table.PrimaryKey("PK_StatusCode_UniqueID", x => x.UniqueID)
        .Annotation("SqlServer:Clustered", false);
    

    并且在应用时会创建非集群PK。

    【讨论】:

    • 谢谢你,Ivan,你所说的重要的事情是......“并重新生成迁移” 一切正常,谢谢你的帮助 :-)
    猜你喜欢
    • 2017-04-21
    • 2016-12-21
    • 2014-07-30
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多