以是,永久不要为你的实体利用 Guid.NewGuid() 创建ID!.
这个问题的一个好的办理方案是天生连续的GUID,由ABP框架供应的开箱即用的. IGuidGenerator 做事创建顺序的GUID(默认由 SequentialGuidGenerator 实现). 当须要手动设置实体的Id时,请利用 IGuidGenerator.Create().
示例: 具有GUID主键的实体并创建该实体

定义一个具有 Guid 主键的 Product 实体:
using System;using Volo.Abp.Domain.Entities;namespace AbpDemo{ public class Product : AggregateRoot<Guid> { public string Name { get; set; } private Product() { / This constructor is used by the ORM/database provider / } public Product(Guid id, string name) : base(id) { Name = name; } }}
创建一个产品:
using System;using System.Threading.Tasks;using Volo.Abp.DependencyInjection;using Volo.Abp.Domain.Repositories;using Volo.Abp.Guids;namespace AbpDemo{ public class MyProductService : ITransientDependency { private readonly IRepository<Product, Guid> _productRepository; private readonly IGuidGenerator _guidGenerator; public MyProductService(IRepository<Product, Guid> productRepository,IGuidGenerator guidGenerator) { _productRepository = productRepository; _guidGenerator = guidGenerator; } public async Task CreateAsync(string productName) { var product = new Product(_guidGenerator.Create(), productName); await _productRepository.InsertAsync(product); } }}
该做事将 IGuidGenerator 注入布局函数中. 如果你的类是运用做事或派生自其他基类之一,可以直策应用 GuidGenerator 基类属性,该属性是预先注入的IGuidGenerator 实例.
AbpSequentialGuidGeneratorOptionsAbpSequentialGuidGeneratorOptions 是用于配置顺序天生GUID的选项类. 它只有一个属性:
DefaultSequentialGuidType (SequentialGuidType 类型的列举): 天生GUID值时利用的策略.数据库供应程序在处理GUID时的行为有所不同,该当根据数据库供应程序进行设置.
SequentialGuidType 有以下列举成员:
SequentialAtEnd (default) 用于SQL Server.SequentialAsString 用于MySQL和PostgreSQL.SequentialAsBinary 用于Oracle.在你的模块的 ConfigureServices 方法配置选项,如下:
Configure<AbpSequentialGuidGeneratorOptions>(options =>{ options.DefaultSequentialGuidType = SequentialGuidType.SequentialAsBinary;});
EF Core集成包已为干系的DBMS设置相应的值. 如果你正在利用这些集成包,在大多数情形下则无需设置此选项.