如何支持数据库实体定义雪花ID并自动赋值 #31
-
目前使用FullAuditEntity,long类型的需要设置为非自增且需要手动赋值,不支持自动获取雪花ID赋值,建议能够支持雪花ID赋值 |
Beta Was this translation helpful? Give feedback.
Answered by
luoyunchong
Mar 7, 2023
Replies: 2 comments 6 replies
-
mysql下主键自增也能赋值,
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" /> -(新增)FreeSqlBuilder增加优先级,按顺序覆盖的逻辑,最后的那个优先级最高 UseMappingPriority(MappingPriorityType.FluentApi, MappingPriorityType.Attribute, MappingPriorityType.Aop)
var options = new IdGeneratorOptions(1);
// options.WorkerIdBitLength = 10; // 默认值6,限定 WorkerId 最大值为2^6-1,即默认最多支持64个节点。
// options.SeqBitLength = 6; // 默认值6,限制每毫秒生成的ID个数。若生成速度超过5万个/秒,建议加大 SeqBitLength 到 10。
// options.BaseTime = Your_Base_Time; // 如果要兼容老系统的雪花算法,此处应设置为老系统的BaseTime。
// ...... 其它参数参考 IdGeneratorOptions 定义。
YitIdHelper.SetIdGenerator(options);
// 保存参数(务必调用,否则参数设置不生效)
fsql.Aop.AuditValue += (s, e) =>
{
if (e.Column.CsType == typeof(long) && e.Property.Name == "Id" && e.Value?.ToString() == "0")
{
e.Value = YitIdHelper.NextId();
}
};
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if ( e.Property.Name == "Id")
e.ModifyResult.IsIdentity = false;
}; |
Beta Was this translation helpful? Give feedback.
5 replies
-
或第二种方式 FreeSqlBuilder配置优先级,按顺序覆盖的逻辑,最后的那个优先级最高 UseMappingPriority(MappingPriorityType.FluentApi, MappingPriorityType.Attribute, MappingPriorityType.Aop) fsql.Aop.AuditValue += (s, e) =>
{
if (e.Column.CsType == typeof(long) && e.Property.Name == "Id" && e.Value?.ToString() == "0")
{
e.Value = YitIdHelper.NextId();
}
};
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if ( e.Property.Name == "Id")
e.ModifyResult.IsIdentity = false;
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fzwu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
或第二种方式
FreeSqlBuilder配置优先级,按顺序覆盖的逻辑,最后的那个优先级最高