SqlSugar 實踐筆記

使用 Insertable + ExecuteReturnSnowflakeIdAsync 爲Id賦值時Id字段不能自增

數據庫爲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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章