From 2063a547eaf4653cc6039ea49ce1aeeabc9d8838 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 5 Aug 2017 13:01:12 +0800 Subject: [PATCH] Insertable Add Return Entity --- Src/Asp.Net/SqlServerTest/Demos/3_Insert.cs | 12 +++++++----- Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs | 2 +- Src/Asp.Net/SqlServerTest/UnitTest/EnumTest.cs | 2 +- .../Abstract/EntityProvider/EntityProvider.cs | 6 +++++- .../InsertableProvider/InsertableProvider.cs | 15 +++++++++++++-- Src/Asp.Net/SqlSugar/Interface/Insertable.cs | 3 ++- Src/Asp.Net/SqlSugar/SimpleClient.cs | 2 +- 7 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demos/3_Insert.cs b/Src/Asp.Net/SqlServerTest/Demos/3_Insert.cs index c9e52329a..f3d162dae 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/3_Insert.cs +++ b/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 diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs b/Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs index 790df43b4..fab8e7c11 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs @@ -38,7 +38,7 @@ namespace OrmTest.UnitTest Varbinary2 = null, String = "string" }; - var id = db.Insertable(insertObject).ExecuteReutrnIdentity(); + var id = db.Insertable(insertObject).ExecuteReturnIdentity(); var data = db.Queryable().InSingle(id); if ( insertObject.Datetime1.ToString("yyyy-MM-dd HH:mm:ss") != data.Datetime1.ToString("yyyy-MM-dd HH:mm:ss") || diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/EnumTest.cs b/Src/Asp.Net/SqlServerTest/UnitTest/EnumTest.cs index 26515076b..8da966c05 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/EnumTest.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/EnumTest.cs @@ -28,7 +28,7 @@ namespace OrmTest.UnitTest SchoolId = shoolValue }; var x2 = db.Queryable().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().AS("student").InSingle(id); shoolValue = SchoolEnum.UniversityOfOxford; var sql= db.Updateable().AS("student").UpdateColumns(it=>new StudentEnum() { Name="a" , SchoolId= shoolValue }).Where(it=>it.Id==id).ToSql(); diff --git a/Src/Asp.Net/SqlSugar/Abstract/EntityProvider/EntityProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/EntityProvider/EntityProvider.cs index 987b57819..25020d911 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/EntityProvider/EntityProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/EntityProvider/EntityProvider.cs @@ -100,7 +100,11 @@ namespace SqlSugar return mappingInfo == null ? dbColumnName : mappingInfo.DbColumnName; } } - + public PropertyInfo GetProperty(string dbColumnName) + { + var propertyName = GetPropertyName(dbColumnName); + return typeof(T).GetProperties().First(it => it.Name == propertyName); + } #region Primary key private static void SetColumns(EntityInfo result) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 54962acff..a3b3c7edf 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -45,7 +45,7 @@ namespace SqlSugar RestoreMapping(); return new KeyValuePair>(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(identityKey).SetValue(result,idValue,null); + return result; + } #endregion #region Setting @@ -79,7 +90,7 @@ namespace SqlSugar public IInsertable InsertColumns(Expression> 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; } diff --git a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs index 21307429d..277a8d3ec 100644 --- a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs @@ -10,7 +10,8 @@ namespace SqlSugar public interface IInsertable { int ExecuteCommand(); - int ExecuteReutrnIdentity(); + int ExecuteReturnIdentity(); + T ExecuteReturnEntity(); IInsertable AS(string tableName); IInsertable With(string lockString); IInsertable InsertColumns(Expression> columns); diff --git a/Src/Asp.Net/SqlSugar/SimpleClient.cs b/Src/Asp.Net/SqlSugar/SimpleClient.cs index 9288e979f..ba4499a5a 100644 --- a/Src/Asp.Net/SqlSugar/SimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/SimpleClient.cs @@ -38,7 +38,7 @@ namespace SqlSugar } public int InsertReturnIdentity(T insertObj) where T : class, new() { - return this.Context.Insertable(insertObj).ExecuteReutrnIdentity(); + return this.Context.Insertable(insertObj).ExecuteReturnIdentity(); } public bool InsertRange(T[] insertObjs) where T : class, new() {