Skip to content

Commit ab52728

Browse files
2881028810
authored andcommitted
- 增加 BaseEntity SaveMany 方法;
1 parent b71e7c1 commit ab52728

File tree

7 files changed

+113
-7
lines changed

7 files changed

+113
-7
lines changed

Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ public virtual TEntity Save()
143143
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
144144
return this.Repository.InsertOrUpdate(this as TEntity);
145145
}
146+
147+
/// <summary>
148+
/// 【完整】保存导航属性,子表
149+
/// </summary>
150+
/// <param name="navigatePropertyName">导航属性名</param>
151+
public virtual void SaveMany(string navigatePropertyName)
152+
{
153+
if (this.Repository == null)
154+
this.Repository = Orm.GetRepository<TEntity>();
155+
156+
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
157+
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
158+
}
146159
}
147160
}
148161

Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ public virtual Task<TEntity> SaveAsync()
125125
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
126126
return this.Repository.InsertOrUpdateAsync(this as TEntity);
127127
}
128+
129+
/// <summary>
130+
/// 【完整】保存导航属性,子表
131+
/// </summary>
132+
/// <param name="navigatePropertyName">导航属性名</param>
133+
public virtual Task SaveManyAsync(string navigatePropertyName)
134+
{
135+
if (this.Repository == null)
136+
this.Repository = Orm.GetRepository<TEntity>();
137+
138+
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
139+
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
140+
}
128141
}
129142
}
130143

Extensions/FreeSql.Extensions.BaseEntity/Net40/BaseEntity.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ public virtual TEntity Save()
104104

105105
return this.Repository.InsertOrUpdate(this as TEntity);
106106
}
107+
108+
/// <summary>
109+
/// 【完整】保存导航属性,子表
110+
/// </summary>
111+
/// <param name="navigatePropertyName">导航属性名</param>
112+
public virtual void SaveMany(string navigatePropertyName)
113+
{
114+
if (this.Repository == null)
115+
this.Repository = Orm.GetRepository<TEntity>();
116+
117+
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
118+
}
107119
}
108120
}
109121

FreeSql.DbContext/DbSet/DbSetAsync.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,15 @@ async public Task SaveManyAsync(TEntity item, string propertyName)
174174
else whereParentExp = Expression.AndAlso(whereParentExp, whereExp);
175175
}
176176
var propValEach = GetItemValue(item, prop) as IEnumerable;
177-
await _db.Orm.Delete<object>().AsType(tref.RefEntityType)
177+
var subDelete = _db.Orm.Delete<object>().AsType(tref.RefEntityType)
178178
.WithTransaction(_uow?.GetOrBeginTransaction())
179-
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam))
180-
.WhereDynamic(propValEach, true).ExecuteAffrowsAsync();
179+
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam));
180+
foreach (var propValItem in propValEach)
181+
{
182+
subDelete.WhereDynamic(propValEach, true);
183+
break;
184+
}
185+
await subDelete.ExecuteAffrowsAsync();
181186
}
182187
}
183188
finally

FreeSql.DbContext/DbSet/DbSetSync.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,15 @@ public void SaveMany(TEntity item, string propertyName)
186186
else whereParentExp = Expression.AndAlso(whereParentExp, whereExp);
187187
}
188188
var propValEach = GetItemValue(item, prop) as IEnumerable;
189-
_db.Orm.Delete<object>().AsType(tref.RefEntityType)
189+
var subDelete = _db.Orm.Delete<object>().AsType(tref.RefEntityType)
190190
.WithTransaction(_uow?.GetOrBeginTransaction())
191-
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam))
192-
.WhereDynamic(propValEach, true).ExecuteAffrows();
191+
.Where(Expression.Lambda<Func<object, bool>>(whereParentExp, deleteWhereParentParam));
192+
foreach (var propValItem in propValEach)
193+
{
194+
subDelete.WhereDynamic(propValEach, true);
195+
break;
196+
}
197+
subDelete.ExecuteAffrows();
193198
}
194199
}
195200
finally

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,57 @@ class Goods
335335
public Guid CagetoryId { get; set; }
336336
public string Name { get; set; }
337337
}
338+
[Fact]
339+
public void SaveMany_OneToMany()
340+
{
341+
var repo = g.sqlite.GetRepository<Cagetory>();
342+
repo.DbContextOptions.EnableAddOrUpdateNavigateList = false; //关闭级联保存功能
343+
var cts = new[] {
344+
new Cagetory
345+
{
346+
Name = "分类1",
347+
Goodss = new List<Goods>(new[]
348+
{
349+
new Goods { Name = "商品1" },
350+
new Goods { Name = "商品2" },
351+
new Goods { Name = "商品3" }
352+
})
353+
},
354+
new Cagetory
355+
{
356+
Name = "分类2",
357+
Goodss = new List<Goods>(new[]
358+
{
359+
new Goods { Name = "商品4" },
360+
new Goods { Name = "商品5" }
361+
})
362+
}
363+
};
364+
repo.Insert(cts);
365+
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
366+
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
367+
cts[0].Goodss.RemoveAt(1);
368+
cts[1].Goodss.RemoveAt(1);
369+
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
370+
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
371+
372+
cts[0].Name = "分类11";
373+
cts[0].Goodss.Clear();
374+
cts[1].Name = "分类22";
375+
cts[1].Goodss.Clear();
376+
repo.Update(cts);
377+
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
378+
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
379+
cts[0].Name = "分类111";
380+
cts[0].Goodss.Clear();
381+
cts[0].Goodss.Add(new Goods { Name = "商品33" });
382+
cts[1].Name = "分类222";
383+
cts[1].Goodss.Clear();
384+
cts[1].Goodss.Add(new Goods { Name = "商品55" });
385+
repo.Update(cts);
386+
repo.SaveMany(cts[0], "Goodss"); //指定保存 Goodss 一对多属性
387+
repo.SaveMany(cts[1], "Goodss"); //指定保存 Goodss 一对多属性
388+
}
338389

339390
[Fact]
340391
public void EnableAddOrUpdateNavigateList_OneToMany_Parent()
@@ -417,7 +468,7 @@ public void EnableAddOrUpdateNavigateList_ManyToMany()
417468
}
418469
};
419470
var repo = g.sqlite.GetRepository<Song>();
420-
//repo.DbContextOptions.EnableAddOrUpdateNavigateList = false; //关闭级联保存功能
471+
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; //打开级联保存功能
421472
repo.Insert(ss);
422473
//repo.SaveMany(ss[0], "Tags"); //指定保存 Tags 多对多属性
423474

0 commit comments

Comments
 (0)