From e26dbfe5262adbc2b92ecdb04cee8ed41428bc21 Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Sat, 16 Nov 2019 01:47:04 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20DbContext=E3=80=81Repo?=
=?UTF-8?q?sitory=20SaveManyToMany=20=E6=96=B9=E6=B3=95=EF=BC=8C=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0=E6=89=8B=E5=B7=A5=E4=BF=9D=E5=AD=98=20ManyToMany=20?=
=?UTF-8?q?=E5=85=B3=E8=81=94=E6=95=B0=E6=8D=AE=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql.DbContext/DbContext/DbContext.cs | 8 +++
FreeSql.DbContext/DbSet/DbSetAsync.cs | 48 +++++++++++-----
FreeSql.DbContext/DbSet/DbSetSync.cs | 56 ++++++++++++++-----
FreeSql.DbContext/FreeSql.DbContext.xml | 31 +++++++---
.../Repository/Repository/BaseRepository.cs | 6 ++
.../Repository/BaseRepositoryAsync.cs | 10 +++-
.../Repository/Repository/IBasicRepository.cs | 7 +++
.../RepositoryTests.cs | 2 +
8 files changed, 129 insertions(+), 39 deletions(-)
diff --git a/FreeSql.DbContext/DbContext/DbContext.cs b/FreeSql.DbContext/DbContext/DbContext.cs
index 88ef9f85..539f5183 100644
--- a/FreeSql.DbContext/DbContext/DbContext.cs
+++ b/FreeSql.DbContext/DbContext/DbContext.cs
@@ -136,6 +136,13 @@ namespace FreeSql
///
public void AddOrUpdate(TEntity data) where TEntity : class => this.Set().AddOrUpdate(data);
+ ///
+ /// 保存实体的指定 ManyToMany 导航属性
+ ///
+ /// 实体对象
+ /// 属性名
+ public void SaveManyToMany(TEntity data, string propertyName) where TEntity : class => this.Set().SaveManyToMany(data, propertyName);
+
///
/// 附加实体,可用于不查询就更新或删除
///
@@ -163,6 +170,7 @@ namespace FreeSql
public Task UpdateRangeAsync(IEnumerable data) where TEntity : class => this.Set().UpdateRangeAsync(data);
public Task AddOrUpdateAsync(TEntity data) where TEntity : class => this.Set().AddOrUpdateAsync(data);
+ public Task SaveManyToManyAsync(TEntity data, string propertyName) where TEntity : class => this.Set().SaveManyToManyAsync(data, propertyName);
#endif
#endregion
diff --git a/FreeSql.DbContext/DbSet/DbSetAsync.cs b/FreeSql.DbContext/DbSet/DbSetAsync.cs
index de28479d..0adf77f3 100644
--- a/FreeSql.DbContext/DbSet/DbSetAsync.cs
+++ b/FreeSql.DbContext/DbSet/DbSetAsync.cs
@@ -132,44 +132,58 @@ namespace FreeSql
await AddOrUpdateNavigateListAsync(item, true);
}
}
- async Task AddOrUpdateNavigateListAsync(TEntity item, bool isAdd)
+
+ async public Task SaveManyToManyAsync(TEntity item, string propertyName)
+ {
+ if (item == null) return;
+ if (_table.Properties.ContainsKey(propertyName) == false) throw new KeyNotFoundException($"{_table.Type.FullName} 不存在属性 {propertyName}");
+ if (_table.ColumnsByCsIgnore.ContainsKey(propertyName)) throw new ArgumentException($"{_table.Type.FullName} 类型已设置属性 {propertyName} 忽略特性");
+ var tref = _table.GetTableRef(propertyName, true);
+ if (tref.RefType != Internal.Model.TableRefType.ManyToMany) throw new ArgumentException($"{_table.Type.FullName} 类型的属性 {propertyName} 不是 ManyToMany 特性");
+ DbContextExecCommand();
+ var oldEnable = _db.Options.EnableAddOrUpdateNavigateList;
+ _db.Options.EnableAddOrUpdateNavigateList = false;
+ await AddOrUpdateNavigateListAsync(item, false, propertyName);
+ _db.Options.EnableAddOrUpdateNavigateList = oldEnable;
+ }
+ async Task AddOrUpdateNavigateListAsync(TEntity item, bool isAdd, string propertyName = null)
{
Type itemType = null;
- foreach (var prop in _table.Properties)
+ Func action = async prop =>
{
- if (_table.ColumnsByCsIgnore.ContainsKey(prop.Key)) continue;
- if (_table.ColumnsByCs.ContainsKey(prop.Key)) continue;
+ if (_table.ColumnsByCsIgnore.ContainsKey(prop.Name)) return;
+ if (_table.ColumnsByCs.ContainsKey(prop.Name)) return;
- var tref = _table.GetTableRef(prop.Key, true);
- if (tref == null) continue;
+ var tref = _table.GetTableRef(prop.Name, true);
+ if (tref == null) return;
switch (tref.RefType)
{
case Internal.Model.TableRefType.OneToOne:
case Internal.Model.TableRefType.ManyToOne:
- continue;
+ return;
}
object propVal = null;
if (itemType == null) itemType = item.GetType();
if (_table.TypeLazy != null && itemType == _table.TypeLazy)
{
- var lazyField = _dicLazyIsSetField.GetOrAdd(_table.TypeLazy, tl => new ConcurrentDictionary()).GetOrAdd(prop.Key, propName =>
+ var lazyField = _dicLazyIsSetField.GetOrAdd(_table.TypeLazy, tl => new ConcurrentDictionary()).GetOrAdd(prop.Name, propName =>
_table.TypeLazy.GetField($"__lazy__{propName}", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
if (lazyField != null)
{
var lazyFieldValue = (bool)lazyField.GetValue(item);
- if (lazyFieldValue == false) continue;
+ if (lazyFieldValue == false) return;
}
- propVal = prop.Value.GetValue(item);
+ propVal = prop.GetValue(item);
}
else
{
- propVal = prop.Value.GetValue(item);
- if (propVal == null) continue;
+ propVal = prop.GetValue(item);
+ if (propVal == null) return;
}
var propValEach = propVal as IEnumerable;
- if (propValEach == null) continue;
+ if (propValEach == null) return;
DbSet