聚集索引和标识列

聚集索引和标识列

本文介绍了EF核心代码优先-聚集索引和标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF Core 2.0创建一个表,其中主键为GUID,聚簇索引为自动递增的INT列.但是我遇到了这个错误:

I am using EF Core 2.0 to create a table in which the primary key is a GUID and the clustered index is an auto-incrementing INT column. But I'm getting this error:

这是用于创建实体和Fluent API的代码.

This is the code for creating entity and Fluent API.

Tenant.cs

public class Tenant : EntityBase
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ClusteredId { get; set; }
    public new Guid TenantId { get; set; }

    public string TenantCode { get; set; }
}

FluentAPI

protected override void OnModelCreating(ModelBuilder builder)
{
        builder.Entity<Tenant>().HasIndex(c => c.TenantId).ForSqlServerIsClustered(false);
        builder.Entity<Tenant>().HasIndex(c => c.ClusteredId).HasName("ClusteredId").ForSqlServerIsClustered(true);

        base.OnModelCreating(builder);
}

请提出建议,如何消除此错误并为GUID创建主键,并为自动递增INT列创建聚簇索引.

Please suggest, how to remove this error and create primary key for GUID and clustered index for auto-incrementing INT column.

谢谢.

推荐答案

PK索引未明确维护.在EF Core中,可以通过KeyBuilder fluent API对其进行配置(请注意,用HasKey代替HasIndex):

The PK index is not maintained explicitly. In EF Core it can be configured via KeyBuilder fluent API (note the HasKey in place of HasIndex):

    builder.Entity<Tenant>().HasKey(c => c.TenantId).ForSqlServerIsClustered(false);
    builder.Entity<Tenant>().HasIndex(c => c.ClusteredId).HasName("ClusteredId").ForSqlServerIsClustered(true);

这篇关于EF核心代码优先-聚集索引和标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 23:44