Browse Source

5.0.0.20

pull/12/MERGE
skx 4 years ago
parent
commit
11e15ac2b0
  1. 22
      Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs
  2. 9
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  3. 102
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs

22
Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs

@ -56,6 +56,9 @@ namespace OrmTest
};
db.Insertable(insertObjs).UseSqlServer().ExecuteBlueCopy();
db.CodeFirst.InitTables<SubInsertTest, SubInsertTestItem, SubInsertTestItem1, SubInsertTestItem2>();
Console.WriteLine("SubInsert Start");
db.Insertable(new Order()
{
Name = "订单 1",
@ -74,8 +77,8 @@ namespace OrmTest
})
.AddSubList(it => it.Items.First().OrderId).ExecuteReturnPrimaryKey();
db.CodeFirst.InitTables<SubInsertTest, SubInsertTestItem, SubInsertTestItem1, SubInsertTestItem2>();
db.Insertable(new SubInsertTest()
db.Insertable(new List<SubInsertTest>() {
new SubInsertTest()
{
Name="aa",
SubInsertTestItem1=new SubInsertTestItem1() {
@ -86,9 +89,22 @@ namespace OrmTest
Name ="item" ,
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.SubInsertTestItem1)
.ExecuteReturnPrimaryKey();
Console.WriteLine("#### Insertable End ####");

9
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -256,14 +256,17 @@ namespace SqlSugar
{
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(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>();
}
string subMemberName;
object sublist;
SubInsertable<T> result = new SubInsertable<T>();
result.GetList(this.InsertObjs,items, out subMemberName, out sublist);
result.InsertObject = this.InsertObjs.First();
result.InsertObjects = this.InsertObjs;
result.Context = this.Context;
result.SubList = new Dictionary<string, object>();
result.SubList.Add(subMemberName, sublist);

102
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs

@ -12,27 +12,35 @@ namespace SqlSugar
internal EntityInfo Entity { get; set; }
internal Dictionary<string, object> SubList { get; set; }
internal SqlSugarProvider Context { get; set; }
internal T InsertObject { get; set; }
internal T [] InsertObjects { get; set; }
internal InsertBuilder InsertBuilder { get; set; }
internal string Pk { get; set; }
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
{
if (InsertObjects != null&&InsertObjects.Count() > 0)
{
string subMemberName;
object sublist;
GetList(new T[] { InsertObject }, items,out subMemberName,out sublist);
GetList(InsertObjects, items, out subMemberName, out sublist);
if (!this.SubList.ContainsKey(subMemberName))
{
this.SubList.Add(subMemberName, sublist);
}
}
return this;
}
public object ExecuteReturnPrimaryKey()
{
if (InsertObjects != null && InsertObjects.Count()>0)
{
int count = 1;
foreach (var InsertObject in InsertObjects)
{
List<ConditionalModel> conModel = new List<ConditionalModel>();
int id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
int count = 1;
object pkValue = null;
var qureyable = this.Context.Queryable<T>();
if (id.ObjToInt() == 0)
@ -61,17 +69,86 @@ namespace SqlSugar
EntityInfo subEntity = null;
if (item.Value is IEnumerable<object>)
{
var list=item.Value as 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
{
return 0;
}
}
public async Task<object> ExecuteReturnPrimaryKeyAsync()
{
if (InsertObjects != null && InsertObjects.Count() > 0)
{
int count = 1;
foreach (var InsertObject in InsertObjects)
{
List<ConditionalModel> conModel = new List<ConditionalModel>();
int id = await this.Context.Insertable(InsertObject).ExecuteReturnIdentityAsync();
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 =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();
var type = list.First().GetType();
this.Context.InitMappingInfo(type);
subEntity=this.Context.EntityMaintenance.GetEntityInfo(type);
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
foreach (var sbItem in list)
{
SetItems(insertDictionary, sbItem, subEntity,item.Key,pkValue);
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
}
}
else if (item.Value.GetType().IsClass())
@ -79,12 +156,19 @@ namespace SqlSugar
var type = item.Value.GetType();
this.Context.InitMappingInfo(type);
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
SetItems(insertDictionary, item.Value, subEntity,item.Key,pkValue);
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
}
count +=await this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommandAsync();
}
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)
{
var lambdaExpression = (items as LambdaExpression).Body;

Loading…
Cancel
Save