Просмотр исходного кода

数据新增操作,默认主键id已经有值,则忽略默认赋值

kenny 3 лет назад
Родитель
Сommit
fcc8662084
1 измененных файлов с 6 добавлено и 1 удалено
  1. 6 1
      Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

+ 6 - 1
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -63,7 +63,12 @@ public static class SqlSugarSetup
                     {
                         // 主键(long类型)-赋值雪花Id
                         if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
-                            entityInfo.SetValue(Yitter.IdGenerator.YitIdHelper.NextId());
+                        {
+                            var keyId = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
+                            //默认有id,则不用生成,有些业务需要提前设置id
+                            if (keyId == null || (long)keyId == 0)
+                                entityInfo.SetValue(Yitter.IdGenerator.YitIdHelper.NextId());
+                        }
                         if (entityInfo.PropertyName == "CreateTime")
                             entityInfo.SetValue(DateTime.Now);
                         if (App.User != null)