Update SaubInsert

This commit is contained in:
skx 2020-11-13 23:00:03 +08:00
parent cdce78f067
commit 4aed1b9ffe
7 changed files with 145 additions and 15 deletions

View File

@ -98,7 +98,8 @@ namespace OrmTest
TwoItem2=new TwoItem2() { TwoItem2=new TwoItem2() {
Id="1", Id="1",
ThreeItem2=new List<ThreeItem2>(){ ThreeItem2=new List<ThreeItem2>(){
new ThreeItem2(){ Name="a", TwoItem2Id="1" } new ThreeItem2(){ Name="a", TwoItem2Id="1" },
new ThreeItem2(){ Id=2, Name="a2", TwoItem2Id="2" }
} }
}, },
TwoItem=new TwoItem() TwoItem=new TwoItem()
@ -130,19 +131,95 @@ namespace OrmTest
} }
}) })
.AddSubList(it => it.TwoItem.RootId) .AddSubList(it => it.TwoItem.RootId)
.AddSubList(it => new GetSubInsertTree() .AddSubList(it => new SubInsertTree()
{ {
Expression = it.TwoItem2.RootId, Expression = it.TwoItem2.RootId,
ChildExpression=new List<GetSubInsertTree>() { ChildExpression=new List<SubInsertTree>() {
new GetSubInsertTree(){ new SubInsertTree(){
Expression=it.TwoItem2.ThreeItem2.First().TwoItem2Id Expression=it.TwoItem2.ThreeItem2.First().TwoItem2Id
} }
} }
}) })
.AddSubList(it => it.TwoItem3) .AddSubList(it => it.TwoItem3)
.ExecuteReturnPrimaryKey(); .ExecuteReturnPrimaryKey();
db.CodeFirst.InitTables<Country, Province, City>();
db.DbMaintenance.TruncateTable("Country");
db.DbMaintenance.TruncateTable("Province");
db.DbMaintenance.TruncateTable("City");
db.Insertable(new List<Country>()
{
new Country(){
Id=1,
Name="中国",
Provinces=new List<Province>(){
new Province{
Id=1001,
Name="江苏",
citys=new List<City>(){
new City(){ Id=1001001, Name="南通" },
new City(){ Id=1001002, Name="南京" }
}
},
new Province{
Id=1002,
Name="上海",
citys=new List<City>(){
new City(){ Id=1002001, Name="徐汇" },
new City(){ Id=1002002, Name="普陀" }
}
},
new Province{
Id=1003,
Name="北京",
citys=new List<City>(){
new City(){ Id=1003001, Name="北京A" },
new City(){ Id=1003002, Name="北京B" }
}
}
}
},
new Country(){
Name="美国",
Id=2,
Provinces=new List<Province>()
{
new Province(){
Name="美国小A",
Id=20001
},
new Province(){
Name="美国小b",
Id=20002
}
}
},
new Country(){
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();
var list= db.Queryable<Country>()
.Mapper(it => it.Provinces, it => it.Provinces.First().CountryId)
.Mapper(it=> {
foreach (var item in it.Provinces)
{
item.citys = db.Queryable<City>().Where(y => y.ProvinceId == item.Id).ToList();
}
})
.ToList();
Console.WriteLine("#### Insertable End ####"); Console.WriteLine("#### Insertable End ####");

View File

@ -18,7 +18,6 @@ namespace OrmTest
[SqlSugar.SugarColumn(IsIgnore = true)] [SqlSugar.SugarColumn(IsIgnore = true)]
public List<TwoItem3> TwoItem3 { get; set; } public List<TwoItem3> TwoItem3 { get; set; }
} }
public class TwoItem public class TwoItem
{ {
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
@ -49,4 +48,32 @@ namespace OrmTest
public string Name { get; set; } public string Name { get; set; }
public string TwoItem2Id { get; set; } public string TwoItem2Id { get; set; }
} }
public class Country
{
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; }
public string Name { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<Province> Provinces { get; set; }
}
public class Province
{
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; }
public string Name { get; set; }
public int CountryId { get; set; }
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<City> citys { get; set; }
}
public class City
{
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; }
public int ProvinceId { get; set; }
public string Name { get; set; }
}
} }

View File

@ -272,9 +272,25 @@ namespace SqlSugar
result.Entity = this.EntityInfo; result.Entity = this.EntityInfo;
return result; return result;
} }
public ISubInsertable<T> AddSubList(Expression<Func<T, GetSubInsertTree>> tree) public ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree)
{ {
return null; Check.Exception(GetPrimaryKeys().Count == 0, typeof(T).Name + " need Primary key");
Check.Exception(GetPrimaryKeys().Count > 1, typeof(T).Name + "Multiple primary keys are not supported");
//Check.Exception(this.InsertObjs.Count() > 1, "SubInserable No Support Insertable(List<T>)");
//Check.Exception(items.ToString().Contains(".First().")==false, items.ToString()+ " not supported ");
if (this.InsertObjs == null || this.InsertObjs.Count() == 0)
{
return new SubInsertable<T>();
}
SubInsertable<T> result = new SubInsertable<T>();
result.InsertObjects = this.InsertObjs;
result.Context = this.Context;
result.SubList = new List<SubInsertTreeExpression>();
result.InsertBuilder = this.InsertBuilder;
result.Pk = GetPrimaryKeys().First();
result.Entity = this.EntityInfo;
result.AddSubList(tree);
return result;
} }
#endregion #endregion

View File

@ -23,7 +23,7 @@ namespace SqlSugar
this.SubList.Add(new SubInsertTreeExpression() { Expression = items }); this.SubList.Add(new SubInsertTreeExpression() { Expression = items });
return this; return this;
} }
public ISubInsertable<T> AddSubList(Expression<Func<T, GetSubInsertTree>> tree) public ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree)
{ {
var lamda = (tree as LambdaExpression); var lamda = (tree as LambdaExpression);
var memInit = lamda.Body as MemberInitExpression; var memInit = lamda.Body as MemberInitExpression;
@ -112,7 +112,13 @@ namespace SqlSugar
MemberExpression subMemberException; MemberExpression subMemberException;
string subMemberName = GetMemberName(item, out subMemberException); string subMemberName = GetMemberName(item, out subMemberException);
string childName = GetChildName(item, subMemberException); string childName = GetChildName(item, subMemberException);
var childList = insertObject.GetType().GetProperty(childName).GetValue(insertObject); var childListProperty = insertObject.GetType().GetProperty(childName);
if (childListProperty == null)
{
childName = subMemberName;
childListProperty = insertObject.GetType().GetProperty(childName);
}
var childList = childListProperty.GetValue(insertObject);
if (childList != null) if (childList != null)
{ {
if (!(childList is IEnumerable<object>)) if (!(childList is IEnumerable<object>))
@ -193,10 +199,14 @@ namespace SqlSugar
{ {
listMember = (subMemberException.Expression as MemberExpression); listMember = (subMemberException.Expression as MemberExpression);
} }
if (listMember == null) if (listMember == null&& item.Expression is LambdaExpression)
{ {
listMember = (item.Expression as LambdaExpression).Body as MemberExpression; listMember = (item.Expression as LambdaExpression).Body as MemberExpression;
} }
if (listMember == null && item.Expression is MemberExpression)
{
listMember = item.Expression as MemberExpression;
}
childName = listMember.Member.Name; childName = listMember.Member.Name;
return childName; return childName;
} }

View File

@ -7,10 +7,10 @@ using System.Threading.Tasks;
namespace SqlSugar namespace SqlSugar
{ {
public class GetSubInsertTree public class SubInsertTree
{ {
public object Expression { get; set; } public object Expression { get; set; }
public List<GetSubInsertTree> ChildExpression { get; set; } public List<SubInsertTree> ChildExpression { get; set; }
} }
internal class SubInsertTreeExpression internal class SubInsertTreeExpression

View File

@ -6,7 +6,7 @@ namespace SqlSugar
public interface ISubInsertable<T> public interface ISubInsertable<T>
{ {
ISubInsertable<T> AddSubList(Expression<Func<T, object>> items); ISubInsertable<T> AddSubList(Expression<Func<T, object>> items);
ISubInsertable<T> AddSubList(Expression<Func<T, GetSubInsertTree>> tree); ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
object ExecuteReturnPrimaryKey(); object ExecuteReturnPrimaryKey();
} }
} }

View File

@ -30,7 +30,7 @@ namespace SqlSugar
IInsertable<T> IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false); IInsertable<T> IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false);
ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey); ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey);
ISubInsertable<T> AddSubList(Expression<Func<T, GetSubInsertTree>> tree); ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
IInsertable<T> EnableDiffLogEvent(object businessData = null); IInsertable<T> EnableDiffLogEvent(object businessData = null);
IInsertable<T> RemoveDataCache(); IInsertable<T> RemoveDataCache();