mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update demo
This commit is contained in:
parent
c681a1fbcf
commit
62bbe8954d
@ -142,7 +142,12 @@ namespace OrmTest
|
|||||||
db.Insertable(new Tree() { Id = 2, Name = "root" }).ExecuteCommand();
|
db.Insertable(new Tree() { Id = 2, Name = "root" }).ExecuteCommand();
|
||||||
db.Insertable(new Tree() { Id = 22, Name = "child3", ParentId = 2 }).ExecuteCommand();
|
db.Insertable(new Tree() { Id = 22, Name = "child3", ParentId = 2 }).ExecuteCommand();
|
||||||
|
|
||||||
var list=db.Queryable<Tree>()
|
// Same property name mapping,Both entities have parentId
|
||||||
|
var list = db.Queryable<Tree>().Mapper(it => it.Parent, it => it.ParentId).ToList();
|
||||||
|
|
||||||
|
|
||||||
|
//If both entities have parentId, I don't want to associate with parentId.
|
||||||
|
var list1 =db.Queryable<Tree>()
|
||||||
//parent=(select * from parent where id=it.parentid)
|
//parent=(select * from parent where id=it.parentid)
|
||||||
.Mapper(it=>it.Parent,it=>it.ParentId, it=>it.Parent.Id)
|
.Mapper(it=>it.Parent,it=>it.ParentId, it=>it.Parent.Id)
|
||||||
//Child=(select * from parent where ParentId=it.id)
|
//Child=(select * from parent where ParentId=it.id)
|
||||||
@ -153,6 +158,28 @@ namespace OrmTest
|
|||||||
|
|
||||||
//one to many
|
//one to many
|
||||||
var list3 = db.Queryable<Order>().Mapper(it => it.Items, it => it.Items.First().OrderId).ToList();
|
var list3 = db.Queryable<Order>().Mapper(it => it.Items, it => it.Items.First().OrderId).ToList();
|
||||||
|
|
||||||
|
//many to many
|
||||||
|
db.CodeFirst.InitTables<A, B, ABMapping>();
|
||||||
|
|
||||||
|
db.Insertable(new A() { Name = "A" }).ExecuteCommand();
|
||||||
|
db.Insertable(new B() { Name = "B" }).ExecuteCommand();
|
||||||
|
db.Insertable(new ABMapping() { AId = 1, BId = 1 }).ExecuteCommand();
|
||||||
|
|
||||||
|
var list4 = db.Queryable<ABMapping>()
|
||||||
|
.Mapper(it => it.A, it => it.AId)
|
||||||
|
.Mapper(it => it.B, it => it.BId).ToList();
|
||||||
|
|
||||||
|
//Manual mode
|
||||||
|
var result = db.Queryable<OrderInfo>().Take(10).Select<ViewOrder>().Mapper((itemModel, cache) =>
|
||||||
|
{
|
||||||
|
var allItems = cache.Get(orderList => {
|
||||||
|
var allIds = orderList.Select(it => it.Id).ToList();
|
||||||
|
return db.Queryable<OrderItem>().Where(it => allIds.Contains(it.OrderId)).ToList();//Execute only once
|
||||||
|
});
|
||||||
|
itemModel.Items = allItems.Where(it => it.OrderId==itemModel.Id).ToList();//Every time it's executed
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
Console.WriteLine("#### End Start ####");
|
Console.WriteLine("#### End Start ####");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,4 +28,27 @@ namespace OrmTest
|
|||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
public List<OrderItem> Items { get; set; }
|
public List<OrderItem> Items { get; set; }
|
||||||
}
|
}
|
||||||
|
public class ABMapping
|
||||||
|
{
|
||||||
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
|
public int AId { get; set; }
|
||||||
|
public int BId { get; set; }
|
||||||
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
public A A { get; set; }
|
||||||
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
public B B { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
public class A
|
||||||
|
{
|
||||||
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
public class B
|
||||||
|
{
|
||||||
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user