Skip to content

Commit b045ac5

Browse files
committed
Add concurrency check
1 parent 7960313 commit b045ac5

File tree

9 files changed

+594
-14
lines changed

9 files changed

+594
-14
lines changed

CoreHome.Admin/Controllers/BlogController.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,28 +315,36 @@ public IActionResult UploadPic()
315315
}
316316
}
317317

318-
private void RecyclingData()
318+
private void RecyclingData(int times = 0)
319319
{
320320
//回收分类
321-
List<Category> noArticleCategories = articleDbContext.Categories.Where(i => i.Articles.Count == 0).ToList();
321+
List<Category> noArticleCategories = [.. articleDbContext.Categories.Where(i => i.Articles.Count == 0)];
322322
noArticleCategories.ForEach(i => articleDbContext.Categories.Remove(i));
323323

324324
//回收标签
325-
List<Tag> noArticleTag = articleDbContext.Tags.Where(i => i.ArticleTags.Count == 0).ToList();
325+
List<Tag> noArticleTag = [.. articleDbContext.Tags.Where(i => i.ArticleTags.Count == 0)];
326326
noArticleTag.ForEach(i => articleDbContext.Tags.Remove(i));
327327

328328
//回收归档月份
329-
List<Month> months = articleDbContext.Months.Where(i => i.Articles.Count == 0).ToList();
329+
List<Month> months = [.. articleDbContext.Months.Where(i => i.Articles.Count == 0)];
330330
months.ForEach(i => articleDbContext.Months.Remove(i));
331331

332-
_ = articleDbContext.SaveChanges();
333-
334332
//回收归档年份
335-
List<Year> years = articleDbContext.Years.Where(i => i.Months.Count == 0).ToList();
333+
List<Year> years = [.. articleDbContext.Years.Where
334+
(
335+
i => i.Months.Count == 0 || (i.Months.Count == 1 && months.Contains(i.Months.First()))
336+
)];
336337
years.ForEach(i => articleDbContext.Years.Remove(i));
337338

338-
_ = articleDbContext.SaveChanges();
339+
try
340+
{
341+
_ = articleDbContext.SaveChanges();
342+
}
343+
catch (Exception)
344+
{
345+
if (times > 2) throw;
346+
RecyclingData(++times);
347+
}
339348
}
340-
341349
}
342350
}

CoreHome.Data/DatabaseContext/ArticleDbContext.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33

44
namespace CoreHome.Data.DatabaseContext
55
{
6-
public class ArticleDbContext : DbContext
6+
public class ArticleDbContext(DbContextOptions options) : DbContext(options)
77
{
8-
public ArticleDbContext(DbContextOptions options) : base(options) { }
9-
108
protected override void OnModelCreating(ModelBuilder modelBuilder)
119
{
1210
_ = modelBuilder.Entity<Year>()
1311
.HasKey(i => i.Value);
1412

13+
_ = modelBuilder.Entity<Year>()
14+
.Property(i => i.RowVersion)
15+
.IsRowVersion();
16+
1517
_ = modelBuilder.Entity<Month>()
1618
.HasOne(i => i.Year)
1719
.WithMany(i => i.Months)
1820
.HasForeignKey(i => i.YearId);
1921

22+
_ = modelBuilder.Entity<Month>()
23+
.Property(i => i.RowVersion)
24+
.IsRowVersion();
25+
2026
_ = modelBuilder.Entity<Article>()
2127
.HasOne(i => i.Month)
2228
.WithMany(i => i.Articles)
@@ -27,6 +33,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2733
.WithMany(i => i.Articles)
2834
.HasForeignKey(i => i.CategoryId);
2935

36+
_ = modelBuilder.Entity<Article>()
37+
.Property(i => i.RowVersion)
38+
.IsRowVersion();
39+
3040
_ = modelBuilder.Entity<ArticleTag>()
3141
.HasKey(i => new { i.ArticleId, i.TagId });
3242

@@ -40,13 +50,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4050
.WithMany(i => i.ArticleTags)
4151
.HasForeignKey(i => i.TagId);
4252

53+
_ = modelBuilder.Entity<ArticleTag>()
54+
.Property(i => i.RowVersion)
55+
.IsRowVersion();
56+
4357
_ = modelBuilder.Entity<Comment>()
4458
.HasOne(i => i.Article)
4559
.WithMany(i => i.Comments)
4660
.HasForeignKey(i => i.ArticleId);
4761

4862
_ = modelBuilder.Entity<Notification>()
4963
.HasKey(i => i.Id);
64+
65+
//foreach (var entityType in modelBuilder.Model.GetEntityTypes())
66+
//{
67+
// var timestampProperty = entityType.FindProperty("RowVersion");
68+
69+
// if (timestampProperty != null)
70+
// {
71+
// modelBuilder.Entity(entityType.ClrType).Property("RowVersion").IsRowVersion();
72+
// }
73+
//}
5074
}
5175

5276
public DbSet<Article> Articles { get; set; }

0 commit comments

Comments
 (0)