diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs index 8107f72c6..490f11028 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs @@ -21,8 +21,10 @@ namespace OrmTest.Demo ConditionalModel(); JoinExp(); Clone(); + WhereClassTest(); } + private static void Clone() { var db = GetInstance(); @@ -119,5 +121,40 @@ namespace OrmTest.Demo Console.WriteLine(ex.Message); } } + + private static void WhereClassTest() + { + var db = GetInstance(); + var list=db.Queryable().WhereClass(new WhereClass() { Id=1 }).ToList(); + //where id=1 + var list2 = db.Queryable().WhereClass(new WhereClass() { Name="a"},ignoreDefaultValue:true).ToList(); + //where name="a" + var list3 = db.Queryable().WhereClass(new WhereClass() { Name = "a" }).ToList(); + //where id=0 and name="a" + var list4 = db.Queryable().WhereClass(new WhereClass() { SchoolId="1", Name = "a" },ignoreDefaultValue:true).ToList(); + //school=1,name=a + var list5= db.Queryable().WhereClass(new WhereClass() { SchoolId = "1", Name = "a" }).ToList(); + //school=1,name=a,id=0 + + var list6 = db.Queryable().WhereClass(new List() { + new WhereClass(){ Name="a",SchoolId="1" }, + new WhereClass(){ Id=1 } + },ignoreDefaultValue:true).ToList(); + //(name=a and schoolid=1) or id=1 + + var list7 = db.Queryable().WhereClass(new List() { + new WhereClass(){ Name="a",SchoolId="1" }, + new WhereClass(){ Id=1 } + }).ToList(); + //(name=a and schoolid=1 and id=0) or id=1 + } + + public class WhereClass{ + + public string Name { get; set; } + public int Id { get; set; } + public string SchoolId { get; set; } + + } } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs index 9915944e7..cf68e62a9 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs @@ -1,4 +1,5 @@ -using OrmTest.Models; +using OrmTest.Demo; +using OrmTest.Models; using System; using System.Collections.Generic; using System.Linq; @@ -11,54 +12,69 @@ namespace OrmTest.Demo public static void Init() { var db = GetInstance(); + db.CurrentConnectionConfig.InitKeyType = SqlSugar.InitKeyType.Attribute; - //auto fill ViewModelStudent3 - var s11 = db.Queryable((st, sc) => st.SchoolId == sc.Id) - .Select().ToList(); + //create tables + db.CodeFirst.SetStringDefaultLength(100).InitTables(typeof(MyOrder),typeof(OrderItem),typeof(Person)); + //init test data + db.DbMaintenance.TruncateTable("MyOrder"); + db.DbMaintenance.TruncateTable("OrderItem"); + db.DbMaintenance.TruncateTable("Person"); + db.Insertable(new MyOrder() { orderName = "no1", orgId = "org1", masterPersonId=1 }).ExecuteCommand(); + db.Insertable(new MyOrder() { orderName = "no2", orgId = "org2",masterPersonId=2 }).ExecuteCommand(); - var s12 = db.Queryable((st, sc) => st.SchoolId == sc.Id).Select() + db.Insertable(new OrderItem() { masterOrderId=1}).ExecuteCommand(); + db.Insertable(new OrderItem() { masterOrderId=1}).ExecuteCommand(); + db.Insertable(new OrderItem() { masterOrderId=2 }).ExecuteCommand(); + db.Insertable(new OrderItem() { masterOrderId=2 }).ExecuteCommand(); - .Mapper((it, cache) => - { - var allSchools = cache.GetListByPrimaryKeys(vmodel => vmodel.SchoolId); - //sql select shool where id (in(ViewModelStudent3[0].SchoolId , ViewModelStudent3[1].SchoolId...) + db.Insertable(new Person() { orgId = "org1"}).ExecuteCommand(); + db.Insertable(new Person() { orgId ="org1" }).ExecuteCommand(); - //Equal to allSchools - //var allSchools2= cache.Get(list => - // { - // var ids=list.Select(i => it.SchoolId).ToList(); - // return db.Queryable().In(ids).ToList(); - //});Complex writing metho + //demo - /*one to one*/ - //Good performance - it.School = allSchools.FirstOrDefault(i => i.Id == it.SchoolId); - - //Poor performance. - //it.School = db.Queryable().InSingle(it.SchoolId); - - - /*one to many*/ - it.Schools = allSchools.Where(i => i.Id == it.SchoolId).ToList(); - - - /*C# syntax conversion*/ - it.Name = it.Name == null ? "null" : it.Name; - - }).ToList(); - - - var s13 = db.Queryable((st, sc) => st.SchoolId == sc.Id).Select() - - .Mapper((it, cache) => - { - it.Schools = db.Queryable().Where(i => i.Id == it.SchoolId).ToList(); - }).ToList(); + var list= db.Queryable() + .Mapper(it => it.masterPerson, it => it.masterPersonId) + .Mapper(it => it.Persons, it => it.orgId) + .Mapper(it => it.OrderItems, it => it.OrderItems.First().masterOrderId) + .Mapper(it => it.OrderItemSignle, it => it.OrderItemSignle.masterOrderId) + .Mapper(it => { + it.orderName = it.orderName + "aa";// + }) + .ToList(); + } + public class MyOrder + { + [SqlSugar.SugarColumn(IsPrimaryKey=true,IsIdentity =true)] + public int orderId { get; set; } + public string orderName { get; set; } + public string orgId { get; set; } + public int masterPersonId { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public List OrderItems { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public OrderItem OrderItemSignle { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public List Persons { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public Person masterPerson { get; set; } } + public class Person + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int PersonId { get; set; } + public string orgId { get; set; } + } + public class OrderItem + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int itemId { get; set; } + public int masterOrderId { get; set; } + } } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/IInsertOrUpdate.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/IInsertOrUpdate.cs new file mode 100644 index 000000000..44aa0ff5c --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/IInsertOrUpdate.cs @@ -0,0 +1,40 @@ +using OrmTest.Demo; +using OrmTest.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest.Demo +{ + public class IInsertOrUpdate : DemoBase + { + public static void Init() + { + var db = GetInstance(); + var entity= db.Insertable(new Student() { Name = "abc" }).ExecuteReturnEntity(); + db.Saveable(entity).ExecuteReturnEntity(); + //UPDATE [STudent] SET + //[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE[Id] = @Id + db.Saveable(new Student() { Name="" }).ExecuteReturnEntity(); + // INSERT INTO[STudent] + //([SchoolId],[Name],[CreateTime]) + // VALUES + //(@SchoolId, @Name, @CreateTime); SELECT SCOPE_IDENTITY(); + + + db.Saveable(new Student() { Name = "" }).InsertColumns(it=>it.Name).ExecuteReturnEntity(); + db.Saveable(new Student() { Name = "" }).InsertIgnoreColumns(it => it.SchoolId).ExecuteReturnEntity(); + db.Saveable(entity).UpdateIgnoreColumns(it=>it.SchoolId).ExecuteReturnEntity(); + db.Saveable(entity).UpdateColumns(it=>new { it.Name,it.CreateTime }).ExecuteReturnEntity(); + + + + db.Saveable(new List() { + entity, + new Student() { Name = "" } + }).ExecuteCommand(); + + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs index a4176b2d2..668ff09ed 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs @@ -54,7 +54,8 @@ namespace OrmTest Demo.AttributeDemo.Init(); Demo.Mapper.Init(); Demo.ExtEntity.Init(); - + Demo.Mapper.Init(); + Demo.IInsertOrUpdate.Init(); /***BUG repair test***/ new BugTest.Bug1().Init(); }