Browse Source

Insertable Add Return Entity

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
2063a547ea
  1. 12
      Src/Asp.Net/SqlServerTest/Demos/3_Insert.cs
  2. 2
      Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs
  3. 2
      Src/Asp.Net/SqlServerTest/UnitTest/EnumTest.cs
  4. 6
      Src/Asp.Net/SqlSugar/Abstract/EntityProvider/EntityProvider.cs
  5. 15
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  6. 3
      Src/Asp.Net/SqlSugar/Interface/Insertable.cs
  7. 2
      Src/Asp.Net/SqlSugar/SimpleClient.cs

12
Src/Asp.Net/SqlServerTest/Demos/3_Insert.cs

@ -21,20 +21,22 @@ namespace OrmTest.Demo
var t2 = db.Insertable(insertObj).ExecuteCommand();
//Insert reutrn Identity Value
var t3 = db.Insertable(insertObj).ExecuteReutrnIdentity();
var t3 = db.Insertable(insertObj).ExecuteReturnIdentity();
//Insert reutrn Identity Value
var t31 = db.Insertable(insertObj).ExecuteReturnEntity();
//Only insert Name and SchoolId
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReutrnIdentity();
var t4_1 = db.Insertable(insertObj).InsertColumns(it => it=="Name"||it== "SchoolId").ExecuteReutrnIdentity();
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReturnIdentity();
var t4_1 = db.Insertable(insertObj).InsertColumns(it => it=="Name"||it== "SchoolId").ExecuteReturnIdentity();
//Ignore TestId
var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReutrnIdentity();
var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReturnIdentity();
//Ignore TestId
var t6 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReutrnIdentity();
var t6 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReturnIdentity();
//Use Lock

2
Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs

@ -38,7 +38,7 @@ namespace OrmTest.UnitTest
Varbinary2 = null,
String = "string"
};
var id = db.Insertable<DataTestInfo>(insertObject).ExecuteReutrnIdentity();
var id = db.Insertable<DataTestInfo>(insertObject).ExecuteReturnIdentity();
var data = db.Queryable<DataTestInfo>().InSingle(id);
if (
insertObject.Datetime1.ToString("yyyy-MM-dd HH:mm:ss") != data.Datetime1.ToString("yyyy-MM-dd HH:mm:ss") ||

2
Src/Asp.Net/SqlServerTest/UnitTest/EnumTest.cs

@ -28,7 +28,7 @@ namespace OrmTest.UnitTest
SchoolId = shoolValue
};
var x2 = db.Queryable<StudentEnum>().AS("student").Where(it => enums.Contains(it.SchoolId)).ToSql();
var id= db.Insertable(x).AS("student").ExecuteReutrnIdentity();
var id= db.Insertable(x).AS("student").ExecuteReturnIdentity();
var data = db.Queryable<StudentEnum>().AS("student").InSingle(id);
shoolValue = SchoolEnum.UniversityOfOxford;
var sql= db.Updateable<StudentEnum>().AS("student").UpdateColumns(it=>new StudentEnum() { Name="a" , SchoolId= shoolValue }).Where(it=>it.Id==id).ToSql();

6
Src/Asp.Net/SqlSugar/Abstract/EntityProvider/EntityProvider.cs

@ -100,7 +100,11 @@ namespace SqlSugar
return mappingInfo == null ? dbColumnName : mappingInfo.DbColumnName;
}
}
public PropertyInfo GetProperty<T>(string dbColumnName)
{
var propertyName = GetPropertyName<T>(dbColumnName);
return typeof(T).GetProperties().First(it => it.Name == propertyName);
}
#region Primary key
private static void SetColumns(EntityInfo result)
{

15
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -45,7 +45,7 @@ namespace SqlSugar
RestoreMapping();
return new KeyValuePair<string, List<SugarParameter>>(sql, InsertBuilder.Parameters);
}
public int ExecuteReutrnIdentity()
public int ExecuteReturnIdentity()
{
InsertBuilder.IsReturnIdentity = true;
PreToSql();
@ -53,6 +53,17 @@ namespace SqlSugar
RestoreMapping();
return Ado.GetInt(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
}
public T ExecuteReturnEntity()
{
var result = InsertObjs.First();
var identityKeys=GetIdentityKeys();
if (identityKeys.Count == 0) return result;
var idValue = ExecuteReturnIdentity();
Check.Exception(identityKeys.Count > 1, "ExecuteReutrnEntity does not support multiple identity keys");
var identityKey = identityKeys.First();
this.Context.EntityProvider.GetProperty<T>(identityKey).SetValue(result,idValue,null);
return result;
}
#endregion
#region Setting
@ -79,7 +90,7 @@ namespace SqlSugar
public IInsertable<T> InsertColumns(Expression<Func<T, object>> columns)
{
var ignoreColumns = InsertBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it=>this.SqlBuilder.GetNoTranslationColumnName(it)).ToList();
var ignoreColumns = InsertBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList();
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => ignoreColumns.Contains(it.PropertyName)).ToList();
return this;
}

3
Src/Asp.Net/SqlSugar/Interface/Insertable.cs

@ -10,7 +10,8 @@ namespace SqlSugar
public interface IInsertable<T>
{
int ExecuteCommand();
int ExecuteReutrnIdentity();
int ExecuteReturnIdentity();
T ExecuteReturnEntity();
IInsertable<T> AS(string tableName);
IInsertable<T> With(string lockString);
IInsertable<T> InsertColumns(Expression<Func<T, object>> columns);

2
Src/Asp.Net/SqlSugar/SimpleClient.cs

@ -38,7 +38,7 @@ namespace SqlSugar
}
public int InsertReturnIdentity<T>(T insertObj) where T : class, new()
{
return this.Context.Insertable(insertObj).ExecuteReutrnIdentity();
return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
}
public bool InsertRange<T>(T[] insertObjs) where T : class, new()
{

Loading…
Cancel
Save