惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
G
Google Developers Blog
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
月光博客
月光博客
B
Blog
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
博客园_首页
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Jina AI
Jina AI
S
SegmentFault 最新的问题
H
Help Net Security
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News

博客园 - 试试手气

Web Worker 入门 若依Ruoyi分离版替换 MyBatis-Plus 若依前端菜单管理中路由地址、组件路径、权限字符的使用 Idea clone 项目推送到自有仓库 Spring Boot —— Spring Security Spring Boot —— 集成文档工具 Spring Boot —— Filter 过滤器 Spring Boot —— Cors 跨域 Spring Boot —— 集成 Druid Spring Boot —— 集成 MyBatis-Plus Spring Boot —— 集成 Springdoc Navicat 使用笔记 Datagrip连接SQLServer失败 Asp.net mvc 笔记 Asp.net Core 笔记 mybatis 笔记 SQLServer 笔记 Asp.net Core 全局异常处理 Asp.net Core 基于Cookie的身份认证
SqlSugar 实践笔记
试试手气 · 2023-03-21 · via 博客园 - 试试手气

使用 Insertable + ExecuteReturnSnowflakeIdAsync 为Id赋值时Id字段不能自增,SqlSugarException The entity sets the primary key and is long

解决方式一:实体定义时,主键不能为可空类型

// 正确
public class EntityExample{
   [SugarColumn(IsPrimaryKey = true, ColumnName = "Id", IsOnlyIgnoreUpdate = true)]
   public long Id {get; set;}
}

// 错误
public class EntityExample{
   [SugarColumn(IsPrimaryKey = true, ColumnName = "Id", IsOnlyIgnoreUpdate = true)]
   public long? Id {get; set;}
}

解决方式二:数据库为SqlServer2012,实体的Id字段标记IsPrimaryKey = true属性,新增时使用雪花算法创建Id并将其赋值给新增记录,此时数据表Id字段不能设置为自增,否则插入失败,报SqlException。因此,打算自己管理Id时就从数据表开始不让其自动分配。

public class EntityExample{
   [SugarColumn(IsPrimaryKey = true, ColumnName = "Id", IsOnlyIgnoreUpdate = true)]
   public long Id {get; set;}
}

long Id = await Db.Insertable<ThirdpartyUserInfoModel>(model)
                .ExecuteReturnSnowflakeIdAsync();