Browse Source

Update Demo

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
70da04c84f
  1. 37
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs
  2. 92
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs
  3. 40
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/IInsertOrUpdate.cs
  4. 3
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs

37
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs

@ -21,8 +21,10 @@ namespace OrmTest.Demo
ConditionalModel(); ConditionalModel();
JoinExp(); JoinExp();
Clone(); Clone();
WhereClassTest();
} }
private static void Clone() private static void Clone()
{ {
var db = GetInstance(); var db = GetInstance();
@ -119,5 +121,40 @@ namespace OrmTest.Demo
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
} }
} }
private static void WhereClassTest()
{
var db = GetInstance();
var list=db.Queryable<Student>().WhereClass(new WhereClass() { Id=1 }).ToList();
//where id=1
var list2 = db.Queryable<Student>().WhereClass(new WhereClass() { Name="a"},ignoreDefaultValue:true).ToList();
//where name="a"
var list3 = db.Queryable<Student>().WhereClass(new WhereClass() { Name = "a" }).ToList();
//where id=0 and name="a"
var list4 = db.Queryable<Student>().WhereClass(new WhereClass() { SchoolId="1", Name = "a" },ignoreDefaultValue:true).ToList();
//school=1,name=a
var list5= db.Queryable<Student>().WhereClass(new WhereClass() { SchoolId = "1", Name = "a" }).ToList();
//school=1,name=a,id=0
var list6 = db.Queryable<Student>().WhereClass(new List<WhereClass>() {
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<Student>().WhereClass(new List<WhereClass>() {
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; }
}
} }
} }

92
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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,54 +12,69 @@ namespace OrmTest.Demo
public static void Init() public static void Init()
{ {
var db = GetInstance(); var db = GetInstance();
db.CurrentConnectionConfig.InitKeyType = SqlSugar.InitKeyType.Attribute;
//auto fill ViewModelStudent3 //create tables
var s11 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id) db.CodeFirst.SetStringDefaultLength(100).InitTables(typeof(MyOrder),typeof(OrderItem),typeof(Person));
.Select<ViewModelStudent3>().ToList();
//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<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>() 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<School>(vmodel => vmodel.SchoolId);
//sql select shool where id (in(ViewModelStudent3[0].SchoolId , ViewModelStudent3[1].SchoolId...)
//Equal to allSchools
//var allSchools2= cache.Get(list =>
// {
// var ids=list.Select(i => it.SchoolId).ToList();
// return db.Queryable<School>().In(ids).ToList();
//});Complex writing metho
/*one to one*/
//Good performance
it.School = allSchools.FirstOrDefault(i => i.Id == it.SchoolId);
//Poor performance.
//it.School = db.Queryable<School>().InSingle(it.SchoolId);
/*one to many*/
it.Schools = allSchools.Where(i => i.Id == it.SchoolId).ToList();
db.Insertable(new Person() { orgId = "org1"}).ExecuteCommand();
db.Insertable(new Person() { orgId ="org1" }).ExecuteCommand();
/*C# syntax conversion*/
it.Name = it.Name == null ? "null" : it.Name;
}).ToList(); //demo
var list= db.Queryable<MyOrder>()
.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();
var s13 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>() }
public class MyOrder
.Mapper((it, cache) =>
{ {
it.Schools = db.Queryable<School>().Where(i => i.Id == it.SchoolId).ToList(); [SqlSugar.SugarColumn(IsPrimaryKey=true,IsIdentity =true)]
}).ToList(); 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<OrderItem> OrderItems { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)]
public OrderItem OrderItemSignle { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<Person> 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; }
}
} }
} }

40
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<Student>(new Student() { Name = "abc" }).ExecuteReturnEntity();
db.Saveable<Student>(entity).ExecuteReturnEntity();
//UPDATE [STudent] SET
//[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE[Id] = @Id
db.Saveable<Student>(new Student() { Name="" }).ExecuteReturnEntity();
// INSERT INTO[STudent]
//([SchoolId],[Name],[CreateTime])
// VALUES
//(@SchoolId, @Name, @CreateTime); SELECT SCOPE_IDENTITY();
db.Saveable<Student>(new Student() { Name = "" }).InsertColumns(it=>it.Name).ExecuteReturnEntity();
db.Saveable<Student>(new Student() { Name = "" }).InsertIgnoreColumns(it => it.SchoolId).ExecuteReturnEntity();
db.Saveable<Student>(entity).UpdateIgnoreColumns(it=>it.SchoolId).ExecuteReturnEntity();
db.Saveable<Student>(entity).UpdateColumns(it=>new { it.Name,it.CreateTime }).ExecuteReturnEntity();
db.Saveable<Student>(new List<Student>() {
entity,
new Student() { Name = "" }
}).ExecuteCommand();
}
}
}

3
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs

@ -54,7 +54,8 @@ namespace OrmTest
Demo.AttributeDemo.Init(); Demo.AttributeDemo.Init();
Demo.Mapper.Init(); Demo.Mapper.Init();
Demo.ExtEntity.Init(); Demo.ExtEntity.Init();
Demo.Mapper.Init();
Demo.IInsertOrUpdate.Init();
/***BUG repair test***/ /***BUG repair test***/
new BugTest.Bug1().Init(); new BugTest.Bug1().Init();
} }

Loading…
Cancel
Save