- 增加 DbContext/Repository ManyToMany联级保存功能(之前已支持OneToMany);

This commit is contained in:
28810
2019-09-29 15:02:08 +08:00
parent 7514000490
commit 33cb3e2dae
11 changed files with 435 additions and 58 deletions

View File

@ -148,14 +148,15 @@ namespace FreeSql
public ExecCommandInfoType actionType { get; set; }
public IDbSet dbSet { get; set; }
public Type stateType { get; set; }
public Type entityType { get; set; }
public object state { get; set; }
}
internal enum ExecCommandInfoType { Insert, Update, Delete }
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
internal int _affrows = 0;
internal void EnqueueAction(ExecCommandInfoType actionType, IDbSet dbSet, Type stateType, object state) =>
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
internal void EnqueueAction(ExecCommandInfoType actionType, IDbSet dbSet, Type stateType, Type entityType, object state) =>
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, entityType = entityType, state = state });
#endregion
~DbContext() => this.Dispose();

View File

@ -97,10 +97,11 @@ namespace FreeSql
if (_actions.Any() == false && states.Any() ||
info != null && oldinfo.actionType != info.actionType ||
info != null && oldinfo.stateType != info.stateType)
info != null && oldinfo.stateType != info.stateType ||
info != null && oldinfo.entityType != info.entityType)
{
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType)
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType && oldinfo.entityType == info.entityType)
{
//最后一个,合起来发送
states.Add(info.state);

View File

@ -5,7 +5,16 @@ namespace FreeSql
{
/// <summary>
/// 是否开启一对多,联级保存功能
/// 是否开启一对多,多对多联级保存功能<para></para>
/// <para></para>
/// 【一对多】模型下, 保存时可联级保存实体的属性集合。出于使用安全考虑我们没做完整对比,只实现实体属性集合的添加或更新操作,所以不会删除实体属性集合的数据。<para></para>
/// 完整对比的功能使用起来太危险,试想下面的场景:<para></para>
/// - 保存的时候,实体的属性集合是空的,如何操作?记录全部删除?<para></para>
/// - 保存的时候,由于数据库中记录非常之多,那么只想保存子表的部分数据,或者只需要添加,如何操作?<para></para>
/// <para></para>
/// 【多对多】模型下,我们对中间表的保存是完整对比操作,对外部实体的操作只作新增(注意不会更新)
/// - 属性集合为空时,删除他们的所有关联数据(中间表)
/// - 属性集合不为空时,与数据库存在的关联数据(中间表)完全对比,计算出应该删除和添加的记录
/// </summary>
public bool EnableAddOrUpdateNavigateList { get; set; } = true;
}

View File

@ -97,10 +97,11 @@ namespace FreeSql
if (_actions.Any() == false && states.Any() ||
info != null && oldinfo.actionType != info.actionType ||
info != null && oldinfo.stateType != info.stateType)
info != null && oldinfo.stateType != info.stateType ||
info != null && oldinfo.entityType != info.entityType)
{
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType)
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType && oldinfo.entityType == info.entityType)
{
//最后一个,合起来发送
states.Add(info.state);