diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs index a22ac828f..123821dff 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs @@ -512,5 +512,105 @@ namespace SqlSugar } #endregion + + #region Insert PkList + + private List InsertPkListWithFunc(EntityColumnInfo pkInfo) + { + InsertBuilder.IsReturnPkList = true; + InsertBuilder.IsNoPage = true; + string sql = _ExecuteCommand(); + sql = this.InsertBuilder.ConvertInsertReturnIdFunc(SqlBuilder.GetTranslationColumnName(pkInfo.DbColumnName), sql); + var result = Ado.SqlQuery(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()); + After(sql, null); + return result; + } + + private List InsertPkListNoFunc(EntityColumnInfo pkInfo) + { + if (this.Ado.Transaction != null) + { + return ReturnDefaultIdentity(pkInfo); + } + else + { + try + { + this.Context.Ado.BeginTran(); + var result = ReturnDefaultIdentity(pkInfo); + this.Context.Ado.CommitTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + } + + private List InsertPkListIdentityCount1(EntityColumnInfo pkInfo) + { + if (pkInfo.UnderType == UtilConstants.IntType) + { + return new List { (Type)(object)this.ExecuteReturnIdentity() }; + } + else + { + return new List { (Type)(object)this.ExecuteReturnBigIdentity() }; + } + } + + private List InsertPkListLong() + { + var list = this.ExecuteReturnSnowflakeIdList(); + try + { + return list.Cast().ToList(); + } + catch + { + Check.ExceptionEasy($"long to ExecuteReturnPkList<{typeof(Type).Name}> error ", $" long 转换成ExecuteReturnPkList<{typeof(Type).Name}>失败"); + return null; + } + } + + private List InsertPkListGuid(EntityColumnInfo pkInfo) + { + Check.ExceptionEasy(pkInfo.UnderType.Name != typeof(Type).Name, $"{pkInfo.UnderType.Name} to ExecuteReturnPkList<{typeof(Type).Name}> error ", $" {pkInfo.UnderType.Name} 转换成ExecuteReturnPkList<{typeof(Type).Name}>失败"); + this.ExecuteCommand(); + List result = new List(); + if (InsertBuilder.DbColumnInfoList.HasValue()) + { + foreach (var item in InsertBuilder.DbColumnInfoList) + { + var isPk = item.DbColumnName.EqualCase(pkInfo.DbColumnName); + if (isPk) + { + result.Add((Type)item.Value); + } + } + } + return result; + } + private List ReturnDefaultIdentity(EntityColumnInfo pkInfo) + { + List result = new List(); + foreach (var item in this.InsertObjs) + { + var insertable = this.Context.Insertable(item) + .InsertColumns(this.InsertBuilder.DbColumnInfoList.Select(it => it.DbColumnName).Distinct().ToArray()); + if (pkInfo.UnderType == UtilConstants.IntType) + { + result.Add((Type)(object)insertable.ExecuteReturnIdentity()); + } + else + { + result.Add((Type)(object)insertable.ExecuteReturnBigIdentity()); + } + } + return result; + } + #endregion } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index f29ef2bea..9f996ac27 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -79,62 +79,26 @@ namespace SqlSugar var isIdEntity = pkInfo.IsIdentity|| (pkInfo.OracleSequenceName.HasValue()&&this.Context.CurrentConnectionConfig.DbType==DbType.Oracle); if (pkInfo.UnderType == UtilConstants.LongType) { - var list = this.ExecuteReturnSnowflakeIdList(); - try - { - return list.Cast().ToList(); - } - catch - { - Check.ExceptionEasy($"long to ExecuteReturnPkList<{typeof(Type).Name}> error ", $" long 转换成ExecuteReturnPkList<{typeof(Type).Name}>失败"); - return null; - } + return InsertPkListLong(); } else if (isIdEntity&&this.InsertObjs.Length==1) { - if (pkInfo.UnderType == UtilConstants.IntType) - { - return new List { (Type)(object)this.ExecuteReturnIdentity() }; - } - else - { - return new List { (Type)(object)this.ExecuteReturnBigIdentity() }; - } + return InsertPkListIdentityCount1(pkInfo); } else if (isIdEntity && this.InsertBuilder.ConvertInsertReturnIdFunc == null) { - Check.ExceptionEasy("The current database does not support batch auto increment", "当前数据库不支持批量返回自增"); - return null; + return InsertPkListNoFunc(pkInfo); } else if (isIdEntity && this.InsertBuilder.ConvertInsertReturnIdFunc != null) { - InsertBuilder.IsReturnPkList = true; - InsertBuilder.IsNoPage = true; - string sql = _ExecuteCommand(); - sql = this.InsertBuilder.ConvertInsertReturnIdFunc(SqlBuilder.GetTranslationColumnName(pkInfo.DbColumnName),sql); - var result = Ado.SqlQuery(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()); - After(sql, null); - return result; + return InsertPkListWithFunc(pkInfo); } else { - Check.ExceptionEasy(pkInfo.UnderType.Name != typeof(Type).Name,$"{pkInfo.UnderType.Name} to ExecuteReturnPkList<{typeof(Type).Name}> error ", $" {pkInfo.UnderType.Name} 转换成ExecuteReturnPkList<{typeof(Type).Name}>失败"); - this.ExecuteCommand(); - List result = new List(); - if (InsertBuilder.DbColumnInfoList.HasValue()) - { - foreach (var item in InsertBuilder.DbColumnInfoList) - { - var isPk = item.DbColumnName.EqualCase(pkInfo.DbColumnName); - if (isPk) - { - result.Add((Type)item.Value); - } - } - } - return result; + return InsertPkListGuid(pkInfo); } } + public virtual int ExecuteReturnIdentity() { if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null)