From c04b7ab1de3ec8180c5b9aa96ea41a200578bf41 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 22 Sep 2023 17:20:18 +0800 Subject: [PATCH] Synchronization code --- .../Abstract/FastestProvider/Private.cs | 4 +++- .../Abstract/FastestProvider/Setting.cs | 6 ++++++ .../SqlSugar/Entities/DbFastestProperties.cs | 1 + Src/Asp.Net/SqlSugar/Interface/IFastest.cs | 1 + .../SqlBuilder/SqlServerFastBuilder.cs | 19 +++++++++++++++++-- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs index 385895e86..528782a33 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs @@ -19,7 +19,9 @@ namespace SqlSugar result.CharacterSet = this.CharacterSet; return result; case DbType.SqlServer: - return new SqlServerFastBuilder(); + var result2= new SqlServerFastBuilder(); + result2.DbFastestProperties.IsOffIdentity = this.IsOffIdentity; + return result2; case DbType.Sqlite: return new SqliteFastBuilder(this.entityInfo); case DbType.Oracle: diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Setting.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Setting.cs index 11f515a75..8321461f5 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Setting.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Setting.cs @@ -14,6 +14,7 @@ namespace SqlSugar private string CacheKeyLike { get; set; } private string CharacterSet { get; set; } private bool IsDataAop { get; set; } + private bool IsOffIdentity { get; set; } public IFastest SetCharacterSet(string CharacterSet) { this.CharacterSet = CharacterSet; @@ -44,6 +45,11 @@ namespace SqlSugar this.Size = size; return this; } + public IFastest OffIdentity() + { + this.IsOffIdentity = true; + return this; + } public SplitFastest SplitTable() { SplitFastest result = new SplitFastest(); diff --git a/Src/Asp.Net/SqlSugar/Entities/DbFastestProperties.cs b/Src/Asp.Net/SqlSugar/Entities/DbFastestProperties.cs index 6459532c1..a84fd1c79 100644 --- a/Src/Asp.Net/SqlSugar/Entities/DbFastestProperties.cs +++ b/Src/Asp.Net/SqlSugar/Entities/DbFastestProperties.cs @@ -10,5 +10,6 @@ namespace SqlSugar { public bool HasOffsetTime { get; set; } public string[] WhereColumns { get; set; } + public bool IsOffIdentity { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IFastest.cs b/Src/Asp.Net/SqlSugar/Interface/IFastest.cs index 91b99412a..1347fe4e4 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IFastest.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IFastest.cs @@ -12,6 +12,7 @@ namespace SqlSugar IFastest RemoveDataCache(string cacheKey); IFastest AS(string tableName); IFastest PageSize(int Size); + IFastest OffIdentity(); IFastest SetCharacterSet(string CharacterSet); IFastest EnableDataAop(); int BulkCopy(List datas); diff --git a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerFastBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerFastBuilder.cs index a8dcae24a..58c8bacb2 100644 --- a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerFastBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerFastBuilder.cs @@ -37,11 +37,26 @@ namespace SqlSugar SqlBulkCopy copy; if (this.Context.Ado.Transaction == null) { - copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection); + if (this.DbFastestProperties?.IsOffIdentity == true) + { + copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.KeepIdentity,null); + } + else + { + copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection); + } } else { - copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction); + if (this.DbFastestProperties?.IsOffIdentity == true) + { + copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.KeepIdentity, (SqlTransaction)this.Context.Ado.Transaction); + } + else + { + + copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction); + } } if (this.Context.Ado.Connection.State == ConnectionState.Closed) {