From 3a3a33913ae5d2f6eec731e0a5621696cdbf2787 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 14 May 2023 17:57:15 +0800 Subject: [PATCH] Force insert autoincrement column --- .../InsertableProvider/InsertableProvider.cs | 7 +++++++ .../Abstract/SqlBuilderProvider/InsertBuilder.cs | 1 + Src/Asp.Net/SqlSugar/Interface/Insertable.cs | 1 + .../SqlServer/SqlBuilder/SqlServerInsertBuilder.cs | 13 ++++++++++--- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 9e804d703..cb43cbf71 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -526,9 +526,16 @@ namespace SqlSugar this.InsertBuilder.TableWithString = lockString; return this; } + public IInsertable OffIdentity() + { + this.IsOffIdentity = true; + this.InsertBuilder.IsOffIdentity = true; + return this; + } public IInsertable IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false) { Check.Exception(this.InsertObjs.Count() > 1&& ignoreNullColumn, ErrorMessage.GetThrowMessage("ignoreNullColumn NoSupport batch insert", "ignoreNullColumn 不支持批量操作")); this.IsOffIdentity = isOffIdentity; + this.InsertBuilder.IsOffIdentity = isOffIdentity; if (this.InsertBuilder.LambdaExpressions == null) this.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig); this.InsertBuilder.IsNoInsertNull = ignoreNullColumn; diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs index aff606602..4d7d3fb00 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs @@ -36,6 +36,7 @@ namespace SqlSugar public virtual bool IsReturnPkList { get; set; } public string AsName { get; set; } + public bool IsOffIdentity { get; set; } #endregion #region SqlTemplate diff --git a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs index 10d34169f..4184b5892 100644 --- a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs @@ -61,5 +61,6 @@ namespace SqlSugar SplitInsertable SplitTable(SplitType splitType); void AddQueue(); IInsertable MySqlIgnore(); + IInsertable OffIdentity(); } } diff --git a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerInsertBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerInsertBuilder.cs index f8c20a86f..41cdd9ac4 100644 --- a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerInsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerInsertBuilder.cs @@ -22,10 +22,11 @@ namespace SqlSugar var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList(); var isSingle = groupList.Count() == 1; string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName))); + var result = ""; if (isSingle) { string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it =>base.GetDbColumn(it,Builder.SqlParameterKeyWord + it.DbColumnName))); - return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString); + result= string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString); } else { @@ -63,13 +64,19 @@ namespace SqlSugar pageIndex++; batchInsetrSql.Append("\r\n;\r\n"); } - var result = batchInsetrSql.ToString(); + result = batchInsetrSql.ToString(); if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer) { result += "select SCOPE_IDENTITY();"; } - return result; } + + if (this.IsOffIdentity) + { + var tableName = this.GetTableNameString; + result= $"SET IDENTITY_INSERT {tableName} ON;" + result.TrimEnd(';') + $";SET IDENTITY_INSERT {tableName} OFF"; ; + } + return result; } public override string FormatDateTimeOffset(object value) {