mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
-
This commit is contained in:
parent
93c76d9052
commit
2add84b3f8
@ -134,7 +134,7 @@ namespace OrmTest
|
||||
.AddSubList(it => new SubInsertTree()
|
||||
{
|
||||
Expression = it.TwoItem2.RootId,
|
||||
ChildExpression=new List<SubInsertTree>() {
|
||||
ChildExpression = new List<SubInsertTree>() {
|
||||
new SubInsertTree(){
|
||||
Expression=it.TwoItem2.ThreeItem2.First().TwoItem2Id
|
||||
}
|
||||
@ -143,7 +143,14 @@ namespace OrmTest
|
||||
.AddSubList(it => it.TwoItem3)
|
||||
.ExecuteReturnPrimaryKey();
|
||||
|
||||
SubNoIdentity(db);
|
||||
SubIdentity(db);
|
||||
Console.WriteLine("#### Insertable End ####");
|
||||
|
||||
}
|
||||
|
||||
private static void SubNoIdentity(SqlSugarClient db)
|
||||
{
|
||||
db.CodeFirst.InitTables<Country, Province, City>();
|
||||
db.DbMaintenance.TruncateTable("Country");
|
||||
db.DbMaintenance.TruncateTable("Province");
|
||||
@ -211,18 +218,96 @@ namespace OrmTest
|
||||
})
|
||||
.ExecuteReturnPrimaryKey();
|
||||
|
||||
var list= db.Queryable<Country>()
|
||||
var list = db.Queryable<Country>()
|
||||
.Mapper(it => it.Provinces, it => it.Provinces.First().CountryId)
|
||||
.Mapper(it=> {
|
||||
.Mapper(it =>
|
||||
{
|
||||
foreach (var item in it.Provinces)
|
||||
{
|
||||
item.citys = db.Queryable<City>().Where(y => y.ProvinceId == item.Id).ToList();
|
||||
}
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
private static void SubIdentity(SqlSugarClient db)
|
||||
{
|
||||
db.CodeFirst.InitTables<Country1, Province1, City1>();
|
||||
db.DbMaintenance.TruncateTable("Country1");
|
||||
db.DbMaintenance.TruncateTable("Province1");
|
||||
db.DbMaintenance.TruncateTable("City1");
|
||||
db.Insertable(new List<Country1>()
|
||||
{
|
||||
new Country1(){
|
||||
Id=1,
|
||||
Name="中国",
|
||||
Provinces=new List<Province1>(){
|
||||
new Province1{
|
||||
Id=1001,
|
||||
Name="江苏",
|
||||
citys=new List<City1>(){
|
||||
new City1(){ Id=1001001, Name="南通" },
|
||||
new City1(){ Id=1001002, Name="南京" }
|
||||
}
|
||||
},
|
||||
new Province1{
|
||||
Id=1002,
|
||||
Name="上海",
|
||||
citys=new List<City1>(){
|
||||
new City1(){ Id=1002001, Name="徐汇" },
|
||||
new City1(){ Id=1002002, Name="普陀" }
|
||||
}
|
||||
},
|
||||
new Province1{
|
||||
Id=1003,
|
||||
Name="北京",
|
||||
citys=new List<City1>(){
|
||||
new City1(){ Id=1003001, Name="北京A" },
|
||||
new City1(){ Id=1003002, Name="北京B" }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
new Country1(){
|
||||
Name="美国",
|
||||
Id=2,
|
||||
Provinces=new List<Province1>()
|
||||
{
|
||||
new Province1(){
|
||||
Name="美国小A",
|
||||
Id=20001
|
||||
},
|
||||
new Province1(){
|
||||
Name="美国小b",
|
||||
Id=20002
|
||||
}
|
||||
}
|
||||
},
|
||||
new Country1(){
|
||||
Name="英国",
|
||||
Id=3
|
||||
}
|
||||
})
|
||||
.AddSubList(it => new SubInsertTree()
|
||||
{
|
||||
Expression = it.Provinces.First().CountryId,
|
||||
ChildExpression = new List<SubInsertTree>() {
|
||||
new SubInsertTree(){
|
||||
Expression=it.Provinces.First().citys.First().ProvinceId
|
||||
}
|
||||
}
|
||||
})
|
||||
.ExecuteReturnPrimaryKey();
|
||||
|
||||
Console.WriteLine("#### Insertable End ####");
|
||||
|
||||
var list = db.Queryable<Country1>()
|
||||
.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -76,4 +76,33 @@ namespace OrmTest
|
||||
public int ProvinceId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class Country1
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
[SqlSugar.SugarColumn(IsIgnore = true)]
|
||||
public List<Province1> Provinces { get; set; }
|
||||
}
|
||||
|
||||
public class Province1
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int CountryId { get; set; }
|
||||
[SqlSugar.SugarColumn(IsIgnore = true)]
|
||||
public List<City1> citys { get; set; }
|
||||
}
|
||||
|
||||
public class City1
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)]
|
||||
public int Id { get; set; }
|
||||
public int ProvinceId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -267,6 +267,7 @@ namespace SqlSugar
|
||||
if (id.ObjToInt() == 0)
|
||||
{
|
||||
var primaryProperty = entityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||
Check.Exception(primaryProperty == null, entityInfo.EntityName + " no primarykey");
|
||||
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
||||
}
|
||||
else
|
||||
|
Loading…
Reference in New Issue
Block a user