Browse Source

Add Rep.InsertReturnEntity

SqlSugar5
sunkaixuan 2 years ago
parent
commit
8f488902a1
  1. 30
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  2. 2
      Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs
  3. 10
      Src/Asp.Net/SqlSugar/SimpleClient.cs

30
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();

2
Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs

@ -44,6 +44,7 @@ namespace SqlSugar
long InsertReturnBigIdentity(T insertObj);
long InsertReturnSnowflakeId(T insertObj);
List<long> InsertReturnSnowflakeId(List<T> insertObjs);
T InsertReturnEntity(T insertObj);
bool IsAny(Expression<Func<T, bool>> whereExpression);
@ -79,6 +80,7 @@ namespace SqlSugar
Task<long> InsertReturnBigIdentityAsync(T insertObj);
Task<long> InsertReturnSnowflakeIdAsync(T insertObj);
Task<List<long>> InsertReturnSnowflakeIdAsync(List<T> insertObjs);
Task<T> InsertReturnEntityAsync(T insertObj);
Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression);
Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);

10
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<T> InsertReturnEntityAsync(T insertObj)
{
return await this.Context.Insertable(insertObj).ExecuteReturnEntityAsync();
}
public virtual async Task<bool> InsertRangeAsync(T[] insertObjs)
{
return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;

Loading…
Cancel
Save