Skip to content

ID 生成

目前框架提供了两种ID生成器,分别是GuidInt64,可以在IEntityTypeConfiguration<>中配置ID生成器,示例如下:

使用 IGuidStronglyTypedId 生成器

支持GuidGuidVersion7两种类型的ID生成器,推荐使用GuidVersion7

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NetCorePal.Extensions.Repository.EntityFrameworkCore;
namespace YourNamespace;

public class OrderEntityTypeConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        //使用Guid作为ID
        builder.Property(x => x.Id).UseGuidValueGenerator();

        //使用Guid Version7 作为ID
        builder.Property(x => x.Id).UseGuidVersion7ValueGenerator();
    }
}

使用雪花ID作为 IInt64StronglyTypedId 生成器 (不推荐)

添加包NetCorePal.Extensions.Repository.EntityFrameworkCore.Snowflake:

dotnet add package NetCorePal.Extensions.Repository.EntityFrameworkCore.Snowflake;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NetCorePal.Extensions.Repository.EntityFrameworkCore;
namespace YourNamespace;

public class OrderEntityTypeConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        //使用Int64作为ID
        builder.Property(x => x.Id).UseSnowFlakeValueGenerator();
    }
}