OpenAuth.Net/OpenAuth.Repository/Core/LongEntity.cs
yubaolee 9c2d044d12 增加雪花算法https://gitee.com/yitter/idgenerator
增加对数据库主键为numberic的支持
2021-03-13 16:21:56 +08:00

32 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.ComponentModel;
using Yitter.IdGenerator;
namespace OpenAuth.Repository.Core
{
/// <summary>
/// 数据库Id为numberic类型的数据实体使用该基类用法同Entity
/// 数据库Id字段为numberic(16,0)或以上长度的整型采用雪花算法生成Id。
/// </summary>
public class LongEntity :BaseEntity
{
[Browsable(false)]
public long Id { get; set; }
public override bool KeyIsNull()
{
return Id == 0;
}
/// <summary>
/// 采用雪花算法计算Id
/// </summary>
public override void GenerateDefaultKeyVal()
{
// 全局初始化设置WorkerId默认最大2^16-1。初始化过程全局只需一次且必须最先设置
var options = new IdGeneratorOptions(){ WorkerId = 1};
IIdGenerator IdHelper = new YitIdGenerator(options);
// 初始化以后就可以在需要的地方调用方法生成ID。
Id = IdHelper.NewLong();
}
}
}