SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.8 KiB

6 years ago
using OrmTest.Demo;
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class Mapper : DemoBase
{
public static void Init()
{
var db = GetInstance();
6 years ago
6 years ago
//create tables
db.CodeFirst.SetStringDefaultLength(100).InitTables(typeof(MyOrder),typeof(OrderItem),typeof(Person));
6 years ago
//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();
6 years ago
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();
6 years ago
db.Insertable(new Person() { orgId = "org1"}).ExecuteCommand();
db.Insertable(new Person() { orgId ="org1" }).ExecuteCommand();
6 years ago
//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.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<OrderItem> OrderItems { 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; }
}
}
}