- 增加 BaseEntity SaveMany 方法;

This commit is contained in:
28810 2020-03-06 13:13:47 +08:00
parent b71e7c1b23
commit ab52728f7f
7 changed files with 113 additions and 7 deletions

View File

@ -143,6 +143,19 @@ namespace FreeSql
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
return this.Repository.InsertOrUpdate(this as TEntity);
}
/// <summary>
/// 【完整】保存导航属性,子表
/// </summary>
/// <param name="navigatePropertyName">导航属性名</param>
public virtual void SaveMany(string navigatePropertyName)
{
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
}
}
}

View File

@ -125,6 +125,19 @@ namespace FreeSql
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
return this.Repository.InsertOrUpdateAsync(this as TEntity);
}
/// <summary>
/// 【完整】保存导航属性,子表
/// </summary>
/// <param name="navigatePropertyName">导航属性名</param>
public virtual Task SaveManyAsync(string navigatePropertyName)
{
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
}
}
}

View File

@ -104,6 +104,18 @@ namespace FreeSql
return this.Repository.InsertOrUpdate(this as TEntity);
}
/// <summary>
/// 【完整】保存导航属性,子表
/// </summary>
/// <param name="navigatePropertyName">导航属性名</param>
public virtual void SaveMany(string navigatePropertyName)
{
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
}
}
}

View File

@ -174,10 +174,15 @@ namespace FreeSql
else whereParentExp = Expression.AndAlso(whereParentExp, whereExp);
}
var propValEach = GetItemValue(item, prop) as IEnumerable;
await _db.Orm.Delete<object>().AsType(tref.RefEntityType)
var subDelete = _db.Orm.Delete<object>().AsType(tref.RefEntityType)
.WithTransaction(_uow?.GetOrBeginTransaction())
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam))
.WhereDynamic(propValEach, true).ExecuteAffrowsAsync();
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam));
foreach (var propValItem in propValEach)
{
subDelete.WhereDynamic(propValEach, true);
break;
}
await subDelete.ExecuteAffrowsAsync();
}
}
finally

View File

@ -186,10 +186,15 @@ namespace FreeSql
else whereParentExp = Expression.AndAlso(whereParentExp, whereExp);
}
var propValEach = GetItemValue(item, prop) as IEnumerable;
_db.Orm.Delete<object>().AsType(tref.RefEntityType)
var subDelete = _db.Orm.Delete<object>().AsType(tref.RefEntityType)
.WithTransaction(_uow?.GetOrBeginTransaction())
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam))
.WhereDynamic(propValEach, true).ExecuteAffrows();
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam));
foreach (var propValItem in propValEach)
{
subDelete.WhereDynamic(propValEach, true);
break;
}
subDelete.ExecuteAffrows();
}
}
finally

View File

@ -110,6 +110,13 @@
清空状态数据
</summary>
</member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)">
<summary>
添加

View File

@ -335,6 +335,57 @@ namespace FreeSql.Tests
public Guid CagetoryId { get; set; }
public string Name { get; set; }
}
[Fact]
public void SaveMany_OneToMany()
{
var repo = g.sqlite.GetRepository<Cagetory>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = false; //关闭级联保存功能
var cts = new[] {
new Cagetory
{
Name = "分类1",
Goodss = new List<Goods>(new[]
{
new Goods { Name = "商品1" },
new Goods { Name = "商品2" },
new Goods { Name = "商品3" }
})
},
new Cagetory
{
Name = "分类2",
Goodss = new List<Goods>(new[]
{
new Goods { Name = "商品4" },
new Goods { Name = "商品5" }
})
}
};
repo.Insert(cts);
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
cts[0].Goodss.RemoveAt(1);
cts[1].Goodss.RemoveAt(1);
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
cts[0].Name = "分类11";
cts[0].Goodss.Clear();
cts[1].Name = "分类22";
cts[1].Goodss.Clear();
repo.Update(cts);
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
cts[0].Name = "分类111";
cts[0].Goodss.Clear();
cts[0].Goodss.Add(new Goods { Name = "商品33" });
cts[1].Name = "分类222";
cts[1].Goodss.Clear();
cts[1].Goodss.Add(new Goods { Name = "商品55" });
repo.Update(cts);
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
}
[Fact]
public void EnableAddOrUpdateNavigateList_OneToMany_Parent()
@ -417,7 +468,7 @@ namespace FreeSql.Tests
}
};
var repo = g.sqlite.GetRepository<Song>();
//repo.DbContextOptions.EnableAddOrUpdateNavigateList = false; //关闭级联保存功能
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; //打开级联保存功能
repo.Insert(ss);
//repo.SaveMany(ss[0], "Tags"); //指定保存 Tags 多对多属性