From 8f488902a1a5aa26a711493fbe84b2e43833ce5f Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 10 Sep 2022 21:12:30 +0800 Subject: [PATCH] Add Rep.InsertReturnEntity --- .../InsertableProvider/InsertableProvider.cs | 30 +++++++++++++++++-- .../SqlSugar/Interface/ISimpleClient.cs | 2 ++ Src/Asp.Net/SqlSugar/SimpleClient.cs | 10 +++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 9f996ac27..3e4aa5e98 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -238,7 +238,20 @@ namespace SqlSugar { var result = InsertObjs.First(); var identityKeys = GetIdentityKeys(); - if (identityKeys.Count == 0) { return this.ExecuteCommand() > 0; } + if (identityKeys.Count == 0) + { + var snowColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey = true && it.UnderType == UtilConstants.LongType); + if (snowColumn!=null) + { + var id = this.ExecuteReturnSnowflakeId(); + snowColumn.PropertyInfo.SetValue(result, id); + return true; + } + else + { + return this.ExecuteCommand() > 0; + } + } var idValue = ExecuteReturnBigIdentity(); Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys"); var identityKey = identityKeys.First(); @@ -290,7 +303,20 @@ namespace SqlSugar { var result = InsertObjs.First(); var identityKeys = GetIdentityKeys(); - if (identityKeys.Count == 0) { return await this.ExecuteCommandAsync() > 0; } + if (identityKeys.Count == 0) + { + var snowColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey = true && it.UnderType == UtilConstants.LongType); + if (snowColumn != null) + { + var id =await this.ExecuteReturnSnowflakeIdAsync(); + snowColumn.PropertyInfo.SetValue(result, id); + return true; + } + else + { + return await this.ExecuteCommandAsync() > 0; + } + } var idValue =await ExecuteReturnBigIdentityAsync(); Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys"); var identityKey = identityKeys.First(); diff --git a/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs index 7b3d89259..bd58d5b83 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs @@ -44,6 +44,7 @@ namespace SqlSugar long InsertReturnBigIdentity(T insertObj); long InsertReturnSnowflakeId(T insertObj); List InsertReturnSnowflakeId(List insertObjs); + T InsertReturnEntity(T insertObj); bool IsAny(Expression> whereExpression); @@ -79,6 +80,7 @@ namespace SqlSugar Task InsertReturnBigIdentityAsync(T insertObj); Task InsertReturnSnowflakeIdAsync(T insertObj); Task> InsertReturnSnowflakeIdAsync(List insertObjs); + Task InsertReturnEntityAsync(T insertObj); Task IsAnyAsync(Expression> whereExpression); Task UpdateAsync(Expression> columns, Expression> whereExpression); diff --git a/Src/Asp.Net/SqlSugar/SimpleClient.cs b/Src/Asp.Net/SqlSugar/SimpleClient.cs index c3c9059f6..66199282e 100644 --- a/Src/Asp.Net/SqlSugar/SimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/SimpleClient.cs @@ -182,6 +182,12 @@ namespace SqlSugar { return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync(); } + + public virtual T InsertReturnEntity(T insertObj) + { + return this.Context.Insertable(insertObj).ExecuteReturnEntity(); + } + public virtual bool InsertRange(T[] insertObjs) { return this.Context.Insertable(insertObjs).ExecuteCommand() > 0; @@ -308,6 +314,10 @@ namespace SqlSugar { return this.Context.Insertable(insertObj).ExecuteReturnBigIdentityAsync(); } + public virtual async Task InsertReturnEntityAsync(T insertObj) + { + return await this.Context.Insertable(insertObj).ExecuteReturnEntityAsync(); + } public virtual async Task InsertRangeAsync(T[] insertObjs) { return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;