This commit is contained in:
skx 2020-11-13 23:31:09 +08:00
parent 93c76d9052
commit 2add84b3f8
3 changed files with 123 additions and 8 deletions

View File

@ -143,7 +143,14 @@ namespace OrmTest
.AddSubList(it => it.TwoItem3) .AddSubList(it => it.TwoItem3)
.ExecuteReturnPrimaryKey(); .ExecuteReturnPrimaryKey();
SubNoIdentity(db);
SubIdentity(db);
Console.WriteLine("#### Insertable End ####");
}
private static void SubNoIdentity(SqlSugarClient db)
{
db.CodeFirst.InitTables<Country, Province, City>(); db.CodeFirst.InitTables<Country, Province, City>();
db.DbMaintenance.TruncateTable("Country"); db.DbMaintenance.TruncateTable("Country");
db.DbMaintenance.TruncateTable("Province"); db.DbMaintenance.TruncateTable("Province");
@ -213,16 +220,94 @@ namespace OrmTest
var list = db.Queryable<Country>() var list = db.Queryable<Country>()
.Mapper(it => it.Provinces, it => it.Provinces.First().CountryId) .Mapper(it => it.Provinces, it => it.Provinces.First().CountryId)
.Mapper(it=> { .Mapper(it =>
{
foreach (var item in it.Provinces) foreach (var item in it.Provinces)
{ {
item.citys = db.Queryable<City>().Where(y => y.ProvinceId == item.Id).ToList(); item.citys = db.Queryable<City>().Where(y => y.ProvinceId == item.Id).ToList();
} }
}) })
.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();
} }
} }
} }

View File

@ -76,4 +76,33 @@ namespace OrmTest
public int ProvinceId { get; set; } public int ProvinceId { get; set; }
public string Name { 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; }
}
} }

View File

@ -267,6 +267,7 @@ namespace SqlSugar
if (id.ObjToInt() == 0) if (id.ObjToInt() == 0)
{ {
var primaryProperty = entityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey); var primaryProperty = entityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey);
Check.Exception(primaryProperty == null, entityInfo.EntityName + " no primarykey");
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject); pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
} }
else else