diff --git a/Src/Asp.Net/PgSqlTest/Demo/DemoH_Snowflake.cs b/Src/Asp.Net/PgSqlTest/Demo/DemoH_Snowflake.cs new file mode 100644 index 000000000..1a06aebe3 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/DemoH_Snowflake.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PgSqlTest.Demo +{ + class DemoH_Snowflake + { + } +} diff --git a/Src/Asp.Net/SqlServerTest/Demo/DemoL_Snowflake.cs b/Src/Asp.Net/SqlServerTest/Demo/DemoL_Snowflake.cs new file mode 100644 index 000000000..eb947ac7c --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Demo/DemoL_Snowflake.cs @@ -0,0 +1,49 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class DemoL_Snowflake + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### Utilities Start ####"); + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.SqlServer, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + AopEvents = new AopEvents + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); + } + } + }); + db.CodeFirst.InitTables(); + Console.WriteLine(db.Queryable().Count()); + var id= db.Insertable(new SnowflakeModel() + { + Name="哈哈" + }).ExecuteReturnSnowflakeId(); + var ids = db.Insertable(db.Queryable().Take(10).ToList()).ExecuteReturnSnowflakeIdList(); + Console.WriteLine(db.Queryable().Count()); + } + } + public class SnowflakeModel + { + [SugarColumn(IsPrimaryKey =true)] + public long Id { get; set; } + + public string Name{get;set; } + } +} diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index a63e9a02c..7a7cec647 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -32,6 +32,7 @@ namespace OrmTest DemoG_SimpleClient.Init(); DemoH_Tenant.Init(); DemoJ_Report.Init(); + DemoL_Snowflake.Init(); //Unit test //NewUnitTest.Init(); diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index f83a28085..3c7a31143 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -69,6 +69,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 8c7622e8e..b1fd19583 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -91,9 +91,10 @@ namespace SqlSugar var entity = this.Context.EntityMaintenance.GetEntityInfo(); var snowProperty=entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); Check.Exception(snowProperty==null, "The entity sets the primary key and is long"); - foreach (var item in this.InsertObjs) + Check.Exception(snowProperty.IsIdentity == true, "SnowflakeId IsIdentity can't true"); + foreach (var item in this.InsertBuilder.DbColumnInfoList.Where(it=>it.PropertyName==snowProperty.PropertyName)) { - snowProperty.PropertyInfo.SetValue(item,id); + item.Value = id; } this.ExecuteCommand(); return id; @@ -104,10 +105,11 @@ namespace SqlSugar var entity = this.Context.EntityMaintenance.GetEntityInfo(); var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); Check.Exception(snowProperty == null, "The entity sets the primary key and is long"); - foreach (var item in InsertObjs) + Check.Exception(snowProperty.IsIdentity == true, "SnowflakeId IsIdentity can't true"); + foreach (var item in this.InsertBuilder.DbColumnInfoList.Where(it => it.PropertyName == snowProperty.PropertyName)) { - var id = SnowFlakeSingle.instance.getID();; - snowProperty.PropertyInfo.SetValue(item, id); + var id = SnowFlakeSingle.instance.getID(); + item.Value = id; result.Add(id); } this.ExecuteCommand(); @@ -119,9 +121,10 @@ namespace SqlSugar var entity = this.Context.EntityMaintenance.GetEntityInfo(); var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); Check.Exception(snowProperty == null, "The entity sets the primary key and is long"); - foreach (var item in this.InsertObjs) + Check.Exception(snowProperty.IsIdentity == true, "SnowflakeId IsIdentity can't true"); + foreach (var item in this.InsertBuilder.DbColumnInfoList.Where(it => it.PropertyName == snowProperty.PropertyName)) { - snowProperty.PropertyInfo.SetValue(item, id); + item.Value = id; } await this.ExecuteCommandAsync(); return id; @@ -132,10 +135,11 @@ namespace SqlSugar var entity = this.Context.EntityMaintenance.GetEntityInfo(); var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); Check.Exception(snowProperty == null, "The entity sets the primary key and is long"); - foreach (var item in InsertObjs) + Check.Exception(snowProperty.IsIdentity == true, "SnowflakeId IsIdentity can't true"); + foreach (var item in this.InsertBuilder.DbColumnInfoList.Where(it => it.PropertyName == snowProperty.PropertyName)) { - var id = SnowFlakeSingle.instance.getID(); ; - snowProperty.PropertyInfo.SetValue(item, id); + var id = SnowFlakeSingle.instance.getID(); + item.Value = id; result.Add(id); } await this.ExecuteCommandAsync();