mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-24 13:03:43 +08:00
5.0.0.20
This commit is contained in:
@@ -56,6 +56,9 @@ namespace OrmTest
|
|||||||
};
|
};
|
||||||
db.Insertable(insertObjs).UseSqlServer().ExecuteBlueCopy();
|
db.Insertable(insertObjs).UseSqlServer().ExecuteBlueCopy();
|
||||||
|
|
||||||
|
db.CodeFirst.InitTables<SubInsertTest, SubInsertTestItem, SubInsertTestItem1, SubInsertTestItem2>();
|
||||||
|
Console.WriteLine("SubInsert Start");
|
||||||
|
|
||||||
db.Insertable(new Order()
|
db.Insertable(new Order()
|
||||||
{
|
{
|
||||||
Name = "订单 1",
|
Name = "订单 1",
|
||||||
@@ -74,8 +77,8 @@ namespace OrmTest
|
|||||||
})
|
})
|
||||||
.AddSubList(it => it.Items.First().OrderId).ExecuteReturnPrimaryKey();
|
.AddSubList(it => it.Items.First().OrderId).ExecuteReturnPrimaryKey();
|
||||||
|
|
||||||
db.CodeFirst.InitTables<SubInsertTest, SubInsertTestItem, SubInsertTestItem1, SubInsertTestItem2>();
|
db.Insertable(new List<SubInsertTest>() {
|
||||||
db.Insertable(new SubInsertTest()
|
new SubInsertTest()
|
||||||
{
|
{
|
||||||
Name="aa",
|
Name="aa",
|
||||||
SubInsertTestItem1=new SubInsertTestItem1() {
|
SubInsertTestItem1=new SubInsertTestItem1() {
|
||||||
@@ -86,9 +89,22 @@ namespace OrmTest
|
|||||||
Name ="item" ,
|
Name ="item" ,
|
||||||
TestId=2
|
TestId=2
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
new SubInsertTest()
|
||||||
|
{
|
||||||
|
Name="aa",
|
||||||
|
SubInsertTestItem1=new SubInsertTestItem1() {
|
||||||
|
a="nn"
|
||||||
|
},
|
||||||
|
SubInsertTestItem=new SubInsertTestItem()
|
||||||
|
{
|
||||||
|
Name ="item" ,
|
||||||
|
TestId=2
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.AddSubList(it => it.SubInsertTestItem1)
|
|
||||||
.AddSubList(it => it.SubInsertTestItem.TestId)
|
.AddSubList(it => it.SubInsertTestItem.TestId)
|
||||||
|
.AddSubList(it => it.SubInsertTestItem1)
|
||||||
.ExecuteReturnPrimaryKey();
|
.ExecuteReturnPrimaryKey();
|
||||||
|
|
||||||
Console.WriteLine("#### Insertable End ####");
|
Console.WriteLine("#### Insertable End ####");
|
||||||
|
@@ -256,14 +256,17 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
Check.Exception(GetPrimaryKeys().Count == 0, typeof(T).Name + " need Primary key");
|
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(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(this.InsertObjs.Count() > 1, "SubInserable No Support Insertable(List<T>)");
|
||||||
//Check.Exception(items.ToString().Contains(".First().")==false, items.ToString()+ " not supported ");
|
//Check.Exception(items.ToString().Contains(".First().")==false, items.ToString()+ " not supported ");
|
||||||
|
if (this.InsertObjs == null || this.InsertObjs.Count() == 0)
|
||||||
|
{
|
||||||
|
return new SubInsertable<T>();
|
||||||
|
}
|
||||||
string subMemberName;
|
string subMemberName;
|
||||||
object sublist;
|
object sublist;
|
||||||
SubInsertable<T> result = new SubInsertable<T>();
|
SubInsertable<T> result = new SubInsertable<T>();
|
||||||
result.GetList(this.InsertObjs,items, out subMemberName, out sublist);
|
result.GetList(this.InsertObjs,items, out subMemberName, out sublist);
|
||||||
result.InsertObject = this.InsertObjs.First();
|
result.InsertObjects = this.InsertObjs;
|
||||||
result.Context = this.Context;
|
result.Context = this.Context;
|
||||||
result.SubList = new Dictionary<string, object>();
|
result.SubList = new Dictionary<string, object>();
|
||||||
result.SubList.Add(subMemberName, sublist);
|
result.SubList.Add(subMemberName, sublist);
|
||||||
|
@@ -12,78 +12,162 @@ namespace SqlSugar
|
|||||||
internal EntityInfo Entity { get; set; }
|
internal EntityInfo Entity { get; set; }
|
||||||
internal Dictionary<string, object> SubList { get; set; }
|
internal Dictionary<string, object> SubList { get; set; }
|
||||||
internal SqlSugarProvider Context { get; set; }
|
internal SqlSugarProvider Context { get; set; }
|
||||||
internal T InsertObject { get; set; }
|
internal T [] InsertObjects { get; set; }
|
||||||
internal InsertBuilder InsertBuilder { get; set; }
|
internal InsertBuilder InsertBuilder { get; set; }
|
||||||
internal string Pk { get; set; }
|
internal string Pk { get; set; }
|
||||||
|
|
||||||
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
||||||
{
|
{
|
||||||
string subMemberName;
|
if (InsertObjects != null&&InsertObjects.Count() > 0)
|
||||||
object sublist;
|
|
||||||
GetList(new T[] { InsertObject }, items,out subMemberName,out sublist);
|
|
||||||
if (!this.SubList.ContainsKey(subMemberName))
|
|
||||||
{
|
{
|
||||||
this.SubList.Add(subMemberName, sublist);
|
string subMemberName;
|
||||||
|
object sublist;
|
||||||
|
GetList(InsertObjects, items, out subMemberName, out sublist);
|
||||||
|
if (!this.SubList.ContainsKey(subMemberName))
|
||||||
|
{
|
||||||
|
this.SubList.Add(subMemberName, sublist);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public object ExecuteReturnPrimaryKey()
|
public object ExecuteReturnPrimaryKey()
|
||||||
{
|
{
|
||||||
List<ConditionalModel> conModel = new List<ConditionalModel>();
|
|
||||||
int id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
|
if (InsertObjects != null && InsertObjects.Count()>0)
|
||||||
int count = 1;
|
|
||||||
object pkValue = null;
|
|
||||||
var qureyable = this.Context.Queryable<T>();
|
|
||||||
if (id.ObjToInt() == 0)
|
|
||||||
{
|
{
|
||||||
var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
|
int count = 1;
|
||||||
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
|
foreach (var InsertObject in InsertObjects)
|
||||||
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
|
{
|
||||||
);
|
List<ConditionalModel> conModel = new List<ConditionalModel>();
|
||||||
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
int id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
|
||||||
qureyable.In(pkValue);
|
object pkValue = null;
|
||||||
|
var qureyable = this.Context.Queryable<T>();
|
||||||
|
if (id.ObjToInt() == 0)
|
||||||
|
{
|
||||||
|
var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
|
||||||
|
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
|
||||||
|
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
|
||||||
|
);
|
||||||
|
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
||||||
|
qureyable.In(pkValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qureyable.In(id);
|
||||||
|
pkValue = id;
|
||||||
|
}
|
||||||
|
var data = qureyable.First();
|
||||||
|
foreach (var item in this.SubList)
|
||||||
|
{
|
||||||
|
|
||||||
|
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
||||||
|
if (item.Value == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
EntityInfo subEntity = null;
|
||||||
|
if (item.Value is IEnumerable<object>)
|
||||||
|
{
|
||||||
|
var list = item.Value as IEnumerable<object>;
|
||||||
|
if (list.Count() == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var type = list.First().GetType();
|
||||||
|
this.Context.InitMappingInfo(type);
|
||||||
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||||
|
foreach (var sbItem in list)
|
||||||
|
{
|
||||||
|
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (item.Value.GetType().IsClass())
|
||||||
|
{
|
||||||
|
var type = item.Value.GetType();
|
||||||
|
this.Context.InitMappingInfo(type);
|
||||||
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||||
|
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
|
||||||
|
}
|
||||||
|
count += this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
qureyable.In(id);
|
return 0;
|
||||||
pkValue = id;
|
|
||||||
}
|
}
|
||||||
var data = qureyable.First();
|
|
||||||
foreach (var item in this.SubList)
|
}
|
||||||
{
|
public async Task<object> ExecuteReturnPrimaryKeyAsync()
|
||||||
|
{
|
||||||
|
|
||||||
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
if (InsertObjects != null && InsertObjects.Count() > 0)
|
||||||
if (item.Value == null)
|
{
|
||||||
|
int count = 1;
|
||||||
|
foreach (var InsertObject in InsertObjects)
|
||||||
{
|
{
|
||||||
continue;
|
List<ConditionalModel> conModel = new List<ConditionalModel>();
|
||||||
}
|
int id = await this.Context.Insertable(InsertObject).ExecuteReturnIdentityAsync();
|
||||||
EntityInfo subEntity = null;
|
object pkValue = null;
|
||||||
if (item.Value is IEnumerable<object>)
|
var qureyable = this.Context.Queryable<T>();
|
||||||
{
|
if (id.ObjToInt() == 0)
|
||||||
var list=item.Value as IEnumerable<object>;
|
|
||||||
if (list.Count() == 0)
|
|
||||||
{
|
{
|
||||||
continue;
|
var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
|
||||||
|
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
|
||||||
|
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
|
||||||
|
);
|
||||||
|
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
||||||
|
qureyable.In(pkValue);
|
||||||
}
|
}
|
||||||
var type= list.First().GetType();
|
else
|
||||||
this.Context.InitMappingInfo(type);
|
|
||||||
subEntity=this.Context.EntityMaintenance.GetEntityInfo(type);
|
|
||||||
foreach (var sbItem in list)
|
|
||||||
{
|
{
|
||||||
SetItems(insertDictionary, sbItem, subEntity,item.Key,pkValue);
|
qureyable.In(id);
|
||||||
|
pkValue = id;
|
||||||
|
}
|
||||||
|
var data =await qureyable.FirstAsync();
|
||||||
|
foreach (var item in this.SubList)
|
||||||
|
{
|
||||||
|
|
||||||
|
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
||||||
|
if (item.Value == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
EntityInfo subEntity = null;
|
||||||
|
if (item.Value is IEnumerable<object>)
|
||||||
|
{
|
||||||
|
var list = item.Value as IEnumerable<object>;
|
||||||
|
if (list.Count() == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var type = list.First().GetType();
|
||||||
|
this.Context.InitMappingInfo(type);
|
||||||
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||||
|
foreach (var sbItem in list)
|
||||||
|
{
|
||||||
|
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (item.Value.GetType().IsClass())
|
||||||
|
{
|
||||||
|
var type = item.Value.GetType();
|
||||||
|
this.Context.InitMappingInfo(type);
|
||||||
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||||
|
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
|
||||||
|
}
|
||||||
|
count +=await this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommandAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (item.Value.GetType().IsClass())
|
return count;
|
||||||
{
|
|
||||||
var type = item.Value.GetType();
|
|
||||||
this.Context.InitMappingInfo(type);
|
|
||||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
|
||||||
SetItems(insertDictionary, item.Value, subEntity,item.Key,pkValue);
|
|
||||||
}
|
|
||||||
count+=this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand();
|
|
||||||
}
|
}
|
||||||
return count;
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void GetList(T[] inserts,Expression<Func<T, object>> items, out string subMemberName, out object sublist)
|
public void GetList(T[] inserts,Expression<Func<T, object>> items, out string subMemberName, out object sublist)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user