diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo3_Insertable.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo3_Insertable.cs index dd7e51b37..bcc976aa3 100644 --- a/Src/Asp.Net/PgSqlTest/Demo/Demo3_Insertable.cs +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo3_Insertable.cs @@ -52,6 +52,94 @@ namespace OrmTest //Use Lock db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand(); + + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable("RootTable0"); + db.DbMaintenance.TruncateTable("TwoItem"); + db.DbMaintenance.TruncateTable("TwoItem2"); + db.DbMaintenance.TruncateTable("TwoItem3"); + db.DbMaintenance.TruncateTable("ThreeItem2"); + Console.WriteLine("SubInsert Start"); + + db.Insertable(new Order() + { + Name = "订单 1", + CustomId = 1, + Price = 100, + CreateTime = DateTime.Now, + Id = 0, + Items = new List() { + new OrderItem(){ + CreateTime=DateTime.Now, + OrderId=0, + Price=1, + ItemId=1 + }, + new OrderItem(){ + CreateTime=DateTime.Now, + OrderId=0, + Price=2, + ItemId=2 + } + } + }) + .AddSubList(it => it.Items.First().OrderId).ExecuteCommand(); + + + + db.Insertable(new List() { + new RootTable0() + { + Name="aa", + TwoItem2=new TwoItem2() { + Id="1", + ThreeItem2=new List(){ + new ThreeItem2(){ Name="a", TwoItem2Id="1" }, + new ThreeItem2(){ Id=2, Name="a2", TwoItem2Id="2" } + } + }, + TwoItem=new TwoItem() + { + Name ="itema" , + RootId=2 + }, + TwoItem3=new List(){ + new TwoItem3(){ Id=0, Name="a",Desc="" }, + + } + }, + new RootTable0() + { + Name="bb", + TwoItem2=new TwoItem2() { + Id="2" + }, + TwoItem=new TwoItem() + { + Name ="itemb" , + RootId=2, + + }, + TwoItem3=new List(){ + new TwoItem3(){ Id=1, Name="b",Desc="" }, + new TwoItem3(){ Id=2, Name="b1",Desc="1" }, + } + } + }) + .AddSubList(it => it.TwoItem.RootId) + .AddSubList(it => new SubInsertTree() + { + Expression = it.TwoItem2.RootId, + ChildExpression = new List() { + new SubInsertTree(){ + Expression=it.TwoItem2.ThreeItem2.First().TwoItem2Id + } + } + }) + .AddSubList(it => it.TwoItem3) + .ExecuteCommand(); + Console.WriteLine("#### Insertable End ####"); } } diff --git a/Src/Asp.Net/PgSqlTest/Models/SubInsertTest.cs b/Src/Asp.Net/PgSqlTest/Models/SubInsertTest.cs new file mode 100644 index 000000000..702f93a93 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/SubInsertTest.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class RootTable0 + { + [SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)] + public int Id { get; set; } + public string Name { get; set; } + [SqlSugar.SugarColumn(IsIgnore =true)] + public TwoItem TwoItem { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public TwoItem2 TwoItem2 { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public List TwoItem3 { get; set; } + } + public class TwoItem + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public int RootId { get; set; } + public string Name { get; set; } + } + public class TwoItem2 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public string Id { get; set; } + public int RootId { get; set; } + [SqlSugar.SugarColumn(IsIgnore =true)] + public List ThreeItem2 { get; set; } + } + public class TwoItem3 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public string Name { get; set; } + public string Desc { get; set; } + } + public class ThreeItem2 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + + public int Id { get; set; } + public string Name { get; set; } + public string TwoItem2Id { get; set; } + } + + public class Country + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public string Name { get; set; } + + [SqlSugar.SugarColumn(IsIgnore = true)] + public List Provinces { get; set; } + } + + public class Province + { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] + public int Id { get; set; } + public string Name { get; set; } + public int CountryId { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public List citys { get; set; } + } + + public class City + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public int ProvinceId { get; set; } + public string Name { get; set; } + } + + + public class Country1 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)] + public int Id { get; set; } + public string Name { get; set; } + + [SqlSugar.SugarColumn(IsIgnore = true)] + public List Provinces { get; set; } + } + + public class Province1 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)] + public int Id { get; set; } + public string Name { get; set; } + public int CountryId { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public List citys { get; set; } + } + + public class City1 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)] + public int Id { get; set; } + public int ProvinceId { get; set; } + public string Name { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj index f7a972a05..9998f37ad 100644 --- a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj +++ b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj @@ -92,6 +92,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs index 230a54e78..32fede984 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs @@ -201,7 +201,15 @@ namespace SqlSugar int id = 0; if (isIdentity) { - id = this.Context.Insertable(insert).AS(tableName).ExecuteReturnIdentity(); + if (this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL) + { + var sqlobj = this.Context.Insertable(insert).AS(tableName).ToSql(); + id = this.Context.Ado.GetInt(sqlobj.Key+ " "+ entityInfo.Columns.First(it=>isIdentity).DbColumnName, sqlobj.Value); + } + else + { + id = this.Context.Insertable(insert).AS(tableName).ExecuteReturnIdentity(); + } if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle&&id==0) { var seqName=entityInfo.Columns.First(it => it.OracleSequenceName.HasValue())?.OracleSequenceName;