EF Core GUID PK,具有用于群集密钥的自动增量INT

时间:2018-11-30 11:10:28

标签: c# entity-framework asp.net-core .net-core entity-framework-core

我知道使用GUID作为PK并不是很理想,因为存在群集(碎片整理)。在这方面,首选自动递增INT。

对于采用EF的集群,首先要进行编码的GUID PK具有自动递增的INT怎么办?

目前,我正在为虚拟的Values模型/实体进行映射

public static class ValuesConfiguration
{
    public static void Configure(EntityTypeBuilder<Values> entityBuilder)
    {
        entityBuilder.ToTable("Values");
        entityBuilder.HasKey(x => x.Id);

        entityBuilder.HasIndex(x => x.Email).IsUnique();

        entityBuilder.Property(x => x.Name).IsRequired().HasMaxLength(50);
        entityBuilder.Property(x => x.Email).IsRequired().HasMaxLength(254);
    }
}

public class MyContext : DbContext
{
    public MyContext (DbContextOptions options)
        : base(options)
    {
        this.ChangeTracker.AutoDetectChangesEnabled = true;
    }

    public DbSet<Values> Values{ get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Configure our mappings
        ValuesConfiguration.Configure(modelBuilder.Entity<Values>());
    }
}

public class Values
{
    public Guid Id { get; set; }
    [Required, StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }
    [Required, StringLength(254)]
    public string Email { get; set; }
}

0 个答案:

没有答案