Update demo

This commit is contained in:
sunkaixuan 2019-06-02 15:18:32 +08:00
parent c681a1fbcf
commit 62bbe8954d
2 changed files with 51 additions and 1 deletions

View File

@ -142,7 +142,12 @@ namespace OrmTest
db.Insertable(new Tree() { Id = 2, Name = "root" }).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)
.Mapper(it=>it.Parent,it=>it.ParentId, it=>it.Parent.Id)
//Child=(select * from parent where ParentId=it.id)
@ -153,6 +158,28 @@ namespace OrmTest
//one to many
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 ####");
}

View File

@ -28,4 +28,27 @@ namespace OrmTest
[SugarColumn(IsIgnore = true)]
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; }
}
}