首页 » 网站建设 » phpmysqlguid技巧_ABP vNext框架文档解读24GUID生成

phpmysqlguid技巧_ABP vNext框架文档解读24GUID生成

访客 2024-11-30 0

扫一扫用手机浏览

文章目录 [+]

以是,永久不要为你的实体利用 Guid.NewGuid() 创建ID!.

这个问题的一个好的办理方案是天生连续的GUID,由ABP框架供应的开箱即用的. IGuidGenerator 做事创建顺序的GUID(默认由 SequentialGuidGenerator 实现). 当须要手动设置实体的Id时,请利用 IGuidGenerator.Create().

phpmysqlguid技巧_ABP vNext框架文档解读24GUID生成

示例: 具有GUID主键的实体并创建该实体

phpmysqlguid技巧_ABP vNext框架文档解读24GUID生成
(图片来自网络侵删)

定义一个具有 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 实例.

AbpSequentialGuidGeneratorOptions

AbpSequentialGuidGeneratorOptions 是用于配置顺序天生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设置相应的值. 如果你正在利用这些集成包,在大多数情形下则无需设置此选项.

标签:

相关文章

微信第三方登录便捷与安全的完美融合

社交平台已成为人们日常生活中不可或缺的一部分。微信作为我国最受欢迎的社交软件之一,拥有庞大的用户群体。为了方便用户在不同平台间切换...

网站建设 2025-02-18 阅读0 评论0

广东高速代码表解码高速公路管理智慧

高速公路作为国家交通动脉,连接着城市与城市,承载着巨大的物流和人流。广东作为我国经济大省,高速公路网络密布,交通流量巨大。为了更好...

网站建设 2025-02-18 阅读0 评论0