mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Add SubInsertable
This commit is contained in:
parent
bd8f269caf
commit
2f5e1e3b02
@ -30,7 +30,7 @@ namespace OrmTest
|
||||
}
|
||||
});
|
||||
|
||||
var insertObj = new Order() { Id = 1, Name = "order1",Price=0 };
|
||||
var insertObj = new Order() { Id = 1, Name = "order1", Price = 0 };
|
||||
var insertObjs = new List<Order> {
|
||||
new Order() { Id = 11, Name = "order11", Price=0 },
|
||||
new Order() { Id = 12, Name = "order12" , Price=0}
|
||||
@ -56,7 +56,43 @@ namespace OrmTest
|
||||
};
|
||||
db.Insertable(insertObjs).UseSqlServer().ExecuteBlueCopy();
|
||||
|
||||
db.Insertable(new Order()
|
||||
{
|
||||
Name = "订单 1",
|
||||
CustomId = 1,
|
||||
Price = 100,
|
||||
CreateTime = DateTime.Now,
|
||||
Id = 0,
|
||||
Items = new List<OrderItem>() {
|
||||
new OrderItem(){
|
||||
CreateTime=DateTime.Now,
|
||||
OrderId=0,
|
||||
Price=1,
|
||||
ItemId=1
|
||||
}
|
||||
}
|
||||
})
|
||||
.AddSubList(it => it.Items.First().OrderId).ExecuteReturnPrimaryKey();
|
||||
|
||||
db.CodeFirst.InitTables<SubInsertTest, SubInsertTestItem, SubInsertTestItem1, SubInsertTestItem2>();
|
||||
db.Insertable(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)
|
||||
.ExecuteReturnPrimaryKey();
|
||||
|
||||
Console.WriteLine("#### Insertable End ####");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
39
Src/Asp.Net/SqlServerTest/Models/SubInsertTest.cs
Normal file
39
Src/Asp.Net/SqlServerTest/Models/SubInsertTest.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class SubInsertTest
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
[SqlSugar.SugarColumn(IsIgnore =true)]
|
||||
public SubInsertTestItem SubInsertTestItem { get; set; }
|
||||
[SqlSugar.SugarColumn(IsIgnore = true)]
|
||||
public SubInsertTestItem1 SubInsertTestItem1 { get; set; }
|
||||
[SqlSugar.SugarColumn(IsIgnore = true)]
|
||||
public List<SubInsertTestItem2> SubInsertTestItem2 { get; set; }
|
||||
}
|
||||
|
||||
public class SubInsertTestItem
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public int TestId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
public class SubInsertTestItem1
|
||||
{
|
||||
public string a { get; set; }
|
||||
}
|
||||
public class SubInsertTestItem2
|
||||
{
|
||||
public int OrderId { get; set; }
|
||||
public int xid { get; set; }
|
||||
public string a { get; set; }
|
||||
}
|
||||
}
|
@ -70,6 +70,7 @@
|
||||
<Compile Include="Models\Custom.cs" />
|
||||
<Compile Include="Models\EntityMapper.cs" />
|
||||
<Compile Include="Models\Mapper.cs" />
|
||||
<Compile Include="Models\SubInsertTest.cs" />
|
||||
<Compile Include="Models\Tree.cs" />
|
||||
<Compile Include="Models\AttributeTable.cs" />
|
||||
<Compile Include="Models\Order.cs" />
|
||||
|
@ -251,6 +251,29 @@ namespace SqlSugar
|
||||
diffModel.DiffType = DiffType.insert;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
||||
{
|
||||
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 ");
|
||||
|
||||
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.Context = this.Context;
|
||||
result.SubList = new Dictionary<string, object>();
|
||||
result.SubList.Add(subMemberName, sublist);
|
||||
result.InsertBuilder = this.InsertBuilder;
|
||||
result.Pk = GetPrimaryKeys().First();
|
||||
result.Entity = this.EntityInfo;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
132
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs
Normal file
132
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SubInserable.cs
Normal file
@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubInsertable<T> : ISubInsertable<T> where T : class, new()
|
||||
{
|
||||
internal EntityInfo Entity { get; set; }
|
||||
internal Dictionary<string, object> SubList { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
internal T InsertObject { get; set; }
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
internal string Pk { get; set; }
|
||||
|
||||
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
||||
{
|
||||
string subMemberName;
|
||||
object sublist;
|
||||
GetList(new T[] { InsertObject }, items,out subMemberName,out sublist);
|
||||
if (!this.SubList.ContainsKey(subMemberName))
|
||||
{
|
||||
this.SubList.Add(subMemberName, sublist);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public object ExecuteReturnPrimaryKey()
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
public void GetList(T[] inserts,Expression<Func<T, object>> items, out string subMemberName, out object sublist)
|
||||
{
|
||||
var lambdaExpression = (items as LambdaExpression).Body;
|
||||
if (lambdaExpression is UnaryExpression)
|
||||
{
|
||||
lambdaExpression = (lambdaExpression as UnaryExpression).Operand;
|
||||
}
|
||||
MemberExpression subMemberException = lambdaExpression as MemberExpression;
|
||||
subMemberName = subMemberException.Member.Name;
|
||||
MemberExpression listMember = null;
|
||||
sublist = null;
|
||||
if (subMemberException.Expression is MethodCallExpression)
|
||||
{
|
||||
listMember = (subMemberException.Expression as MethodCallExpression).Arguments.First() as MemberExpression;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
listMember = (subMemberException.Expression as MemberExpression);
|
||||
}
|
||||
if (listMember == null)
|
||||
{
|
||||
listMember = (items as LambdaExpression).Body as MemberExpression;
|
||||
subMemberName = Guid.NewGuid().ToString();
|
||||
}
|
||||
sublist = inserts.First().GetType().GetProperty(listMember.Member.Name).GetValue(inserts.First());
|
||||
}
|
||||
private void SetItems(Dictionary<string, object> insertDictionary, object sbItem, EntityInfo subEntity,string key,object pkValue)
|
||||
{
|
||||
foreach (var item in subEntity.Columns)
|
||||
{
|
||||
if (item.IsIdentity||item.IsIgnore)
|
||||
continue;
|
||||
if (item.PropertyInfo.Name == key)
|
||||
{
|
||||
insertDictionary.Add(item.DbColumnName, pkValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
insertDictionary.Add(item.DbColumnName, item.PropertyInfo.GetValue(sbItem));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Src/Asp.Net/SqlSugar/Interface/ISubInsertable.cs
Normal file
11
Src/Asp.Net/SqlSugar/Interface/ISubInsertable.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface ISubInsertable<T>
|
||||
{
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> items);
|
||||
object ExecuteReturnPrimaryKey();
|
||||
}
|
||||
}
|
@ -29,6 +29,7 @@ namespace SqlSugar
|
||||
IInsertable<T> IgnoreColumns(params string[]columns);
|
||||
IInsertable<T> IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false);
|
||||
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> SubForeignKey);
|
||||
|
||||
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
||||
IInsertable<T> RemoveDataCache();
|
||||
|
@ -80,6 +80,8 @@
|
||||
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||
<Compile Include="Interface\ISubInsertable.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\SubInserable.cs" />
|
||||
<Compile Include="Abstract\SaveableProvider\SaveableProvider.cs" />
|
||||
<Compile Include="CacheScheme\CacheKeyBuider.cs" />
|
||||
<Compile Include="CacheScheme\CacheSchemeMain.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user