Update .net core project

This commit is contained in:
sunkaixuan
2022-08-03 10:49:02 +08:00
parent e0b250a9ba
commit 8469ccd21e
4 changed files with 90 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ namespace SqlSugar
public SqlSugarProvider _Context { get; set; }
public NavigateType? _NavigateType { get; set; }
public bool IsFirst { get; set; }
public InsertNavOptions _navOptions { get; set; }
public InsertNavProvider<Root, Root> AsNav()
{
@@ -29,9 +30,31 @@ namespace SqlSugar
_ParentPkColumn=this._Context.EntityMaintenance.GetEntityInfo<Root>().Columns.First(it=>it.IsPrimarykey)
};
}
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression,InsertNavOptions options) where TChild : class, new()
{
_navOptions = options;
return _ThenInclude(expression);
}
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new()
{
_navOptions = options;
return _ThenInclude(expression);
}
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{
return _ThenInclude(expression);
}
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{
return _ThenInclude(expression);
}
private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{
var name = ExpressionTool.GetMemberName(expression);
if (this._ParentEntity == null)
{
@@ -61,7 +84,8 @@ namespace SqlSugar
}
return GetResult<TChild>();
}
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new()
private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{
var name = ExpressionTool.GetMemberName(expression);
if (this._ParentEntity == null)
@@ -70,8 +94,8 @@ namespace SqlSugar
IsFirst = true;
}
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
;
;
if (nav.Navigat == null)
{
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");

View File

@@ -81,12 +81,20 @@ namespace SqlSugar
children = children.Distinct().ToList();
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
var insertData = children = x.InsertList.Select(it => it.Item).ToList();
if (_NavigateType == NavigateType.OneToMany&&IsFirst==false)
var IsNoExistsNoInsert = _navOptions != null && _navOptions.OneToManyIfExistsNoInsert == true;
if (_NavigateType == NavigateType.OneToMany && IsFirst == false && IsNoExistsNoInsert == false)
{
var updateData = x.UpdateList.Select(it => it.Item).ToList();
ClearPk(updateData, pkColumn);
insertData.AddRange(updateData);
}
else if (_NavigateType == NavigateType.OneToMany && IsNoExistsNoInsert == true)
{
children = new List<TChild>();
children.AddRange(x.InsertList.Select(it => it.Item).ToList());
var updateData = x.UpdateList.Select(it => it.Item).ToList();
children.AddRange(updateData);
}
Check.ExceptionEasy(pkColumn==null&&NavColumn==null,$"The entity is invalid",$"实体错误无法使用导航");
InitData(pkColumn, insertData);
this._ParentList = children.Cast<object>().ToList();

View File

@@ -31,6 +31,25 @@ namespace SqlSugar
result.Context = this.Context;
return result;
}
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression,InsertNavOptions options) where TChild : class, new()
{
this.Context = insertNavProvider._Context;
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression, options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new()
{
this.Context = insertNavProvider._Context;
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression, options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
}
public class InsertNavTask<Root, T> where T : class, new() where Root : class, new()
{
@@ -60,6 +79,36 @@ namespace SqlSugar
{
return AsNav().ThenInclude(expression);
}
public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression,InsertNavOptions options) where TChild : class, new()
{
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression,options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new()
{
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression, options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression, InsertNavOptions options) where TChild : class, new()
{
return AsNav().ThenInclude(expression, options);
}
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new()
{
return AsNav().ThenInclude(expression, options);
}
public bool ExecuteCommand()
{
var hasTran = this.Context.Ado.Transaction != null;

View File

@@ -16,4 +16,9 @@ namespace SqlSugar
public bool ManyToManyIsUpdateA { get; set; }
public bool ManyToManyIsUpdateB { get; set; }
}
public class InsertNavOptions
{
public bool OneToManyIfExistsNoInsert { get; set; }
}
}