Update Insertable

This commit is contained in:
sunkaixuan
2017-09-01 12:45:47 +08:00
parent 165afa4459
commit 3c6a771900
2 changed files with 25 additions and 4 deletions

View File

@@ -53,17 +53,36 @@ namespace SqlSugar
RestoreMapping(); RestoreMapping();
return Ado.GetInt(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()); return Ado.GetInt(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
} }
public long ExecuteReturnBigIdentity()
{
InsertBuilder.IsReturnIdentity = true;
PreToSql();
string sql = InsertBuilder.ToSqlString();
RestoreMapping();
return Convert.ToInt64( Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()));
}
public T ExecuteReturnEntity() public T ExecuteReturnEntity()
{ {
var result = InsertObjs.First(); var result = InsertObjs.First();
var identityKeys=GetIdentityKeys(); var identityKeys = GetIdentityKeys();
if (identityKeys.Count == 0) return result; if (identityKeys.Count == 0) { this.ExecuteCommand(); return result; }
var idValue = ExecuteReturnIdentity(); var idValue = ExecuteReturnBigIdentity();
Check.Exception(identityKeys.Count > 1, "ExecuteReutrnEntity does not support multiple identity keys"); Check.Exception(identityKeys.Count > 1, "ExecuteReutrnEntity does not support multiple identity keys");
var identityKey = identityKeys.First(); var identityKey = identityKeys.First();
this.Context.EntityProvider.GetProperty<T>(identityKey).SetValue(result,idValue,null); this.Context.EntityProvider.GetProperty<T>(identityKey).SetValue(result, idValue, null);
return result; return result;
} }
public bool ExecuteCommandIdentityIntoEntity()
{
var result = InsertObjs.First();
var identityKeys = GetIdentityKeys();
if (identityKeys.Count == 0) { return this.ExecuteCommand() > 0; }
var idValue = ExecuteReturnBigIdentity();
Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys");
var identityKey = identityKeys.First();
this.Context.EntityProvider.GetProperty<T>(identityKey).SetValue(result, idValue, null);
return idValue>0;
}
#endregion #endregion
#region Setting #region Setting

View File

@@ -12,6 +12,8 @@ namespace SqlSugar
int ExecuteCommand(); int ExecuteCommand();
int ExecuteReturnIdentity(); int ExecuteReturnIdentity();
T ExecuteReturnEntity(); T ExecuteReturnEntity();
bool ExecuteCommandIdentityIntoEntity();
long ExecuteReturnBigIdentity();
IInsertable<T> AS(string tableName); IInsertable<T> AS(string tableName);
IInsertable<T> With(string lockString); IInsertable<T> With(string lockString);
IInsertable<T> InsertColumns(Expression<Func<T, object>> columns); IInsertable<T> InsertColumns(Expression<Func<T, object>> columns);