Update AddSubList

This commit is contained in:
sunkaixuan 2022-04-22 13:06:13 +08:00
parent 0826e12fca
commit e86c82881c
3 changed files with 68 additions and 31 deletions

View File

@ -231,38 +231,58 @@ namespace OrmTest
} }
private static void SubIdentity(SqlSugarClient db) private static void SubIdentity(SqlSugarClient db)
{ {
db.CodeFirst.InitTables<Country1, Province1, City1>(); db.CodeFirst.InitTables<Country1, Province1, City1,Area1>();
db.DbMaintenance.TruncateTable("Country1"); db.DbMaintenance.TruncateTable("Country1");
db.DbMaintenance.TruncateTable("Province1"); db.DbMaintenance.TruncateTable("Province1");
db.DbMaintenance.TruncateTable("City1"); db.DbMaintenance.TruncateTable("City1");
db.Insertable(new List<Country1>() db.DbMaintenance.TruncateTable("Area1");
var list = new List<Country1>()
{ {
new Country1(){ new Country1(){
Id=1, Id=1,
Name="中国", Name="中国",
Provinces=new List<Province1>(){ Provinces=new List<Province1>(){
new Province1{ new Province1{
Id=1001, Id=1001,
Name="江苏", Name="江苏",
citys=new List<City1>(){ citys=new List<City1>(){
new City1(){ Id=1001001, Name="南通" }, new City1(){
new City1(){ Id=1001002, Name="南京" } ProvinceId=1001,
} Name ="昆山",
area=new List<Area1>() {
new Area1(){
CityId=000,
Name="江苏小县城"
}
}
}
},
}, },
new Province1{ new Province1{
Id=1002, Id=1002,
Name="上海", Name="上海",
citys=new List<City1>(){ citys=new List<City1>(){
new City1(){ Id=1002001, Name="徐汇" }, new City1(){
new City1(){ Id=1002002, Name="普陀" } ProvinceId=1002,
Name ="陆家嘴"
}
} }
}, },
new Province1{ new Province1{
Id=1003, Id=1003,
Name="北京", Name="北京",
citys=new List<City1>(){ citys=new List<City1>(){
new City1(){ Id=1003001, Name="北京A" }, new City1(){
new City1(){ Id=1003002, Name="北京B" } ProvinceId=1003 ,
Name ="中官村"
}
} }
} }
} }
@ -286,28 +306,30 @@ namespace OrmTest
Name="英国", Name="英国",
Id=3 Id=3
} }
}) };
//开始插入
db.Insertable(list)
.AddSubList(it => new SubInsertTree() .AddSubList(it => new SubInsertTree()
{ {
Expression = it.Provinces.First().CountryId, Expression = it.Provinces.First().CountryId,//CountryId自动填充
ChildExpression = new List<SubInsertTree>() { ChildExpression = new List<SubInsertTree>() {
new SubInsertTree(){ new SubInsertTree(){
Expression=it.Provinces.First().citys.First().ProvinceId Expression=it.Provinces.First()
} .citys.First()
.ProvinceId,//ProvinceId自动填充,
ChildExpression=new List<SubInsertTree>(){
new SubInsertTree(){
Expression = it.Provinces.First().citys.First().area.First().CityId
}
}
}
} }
}) })
//如果有多个子结果这里还能在.AddSubList
.ExecuteCommand(); .ExecuteCommand();
var list = db.Queryable<Country1>() var list2=db.Queryable<Country1>().Includes(x => x.Provinces, x => x.citys, x => x.area).ToList();
.Mapper(it => it.Provinces, it => it.Provinces.First().CountryId)
.Mapper(it =>
{
foreach (var item in it.Provinces)
{
item.citys = db.Queryable<City1>().Where(y => y.ProvinceId == item.Id).ToList();
}
})
.ToList();
} }
} }
} }

View File

@ -84,7 +84,7 @@ namespace OrmTest
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)] [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany,nameof(Province1.CountryId))]
public List<Province1> Provinces { get; set; } public List<Province1> Provinces { get; set; }
} }
@ -94,7 +94,7 @@ namespace OrmTest
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int CountryId { get; set; } public int CountryId { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)] [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(City1.ProvinceId))]
public List<City1> citys { get; set; } public List<City1> citys { get; set; }
} }
@ -104,5 +104,16 @@ namespace OrmTest
public int Id { get; set; } public int Id { get; set; }
public int ProvinceId { get; set; } public int ProvinceId { get; set; }
public string Name { get; set; } public string Name { get; set; }
[SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Area1.CityId))]
public List<Area1> area { get; set; }
}
public class Area1
{
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public int CityId { get; set; }
public string Name { get; set; }
} }
} }

View File

@ -63,6 +63,10 @@ namespace SqlSugar
{ {
MemberAssignment memberAssignment = (MemberAssignment)memInit.Bindings[0]; MemberAssignment memberAssignment = (MemberAssignment)memInit.Bindings[0];
tree.Expression = memberAssignment.Expression; tree.Expression = memberAssignment.Expression;
if (memInit.Bindings.Count > 1)
{
tree.Childs = GetSubInsertTree(((MemberAssignment)memInit.Bindings[1]).Expression);
}
} }
resul.Add(tree); resul.Add(tree);
} }