-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYCodeLabDbContext.cs
More file actions
47 lines (40 loc) · 1.39 KB
/
Copy pathYCodeLabDbContext.cs
File metadata and controls
47 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using YCodeLab.DB.Messaging;
namespace YCodeLab.DB
{
public class YCodeLabDbContext : DbContext
{
public YCodeLabDbContext(DbContextOptions<YCodeLabDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new MessageEntityConfig());
modelBuilder.ApplyConfiguration(new CommentEntityConfig());
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
PrepareEntitiesBeforeSave();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
PrepareEntitiesBeforeSave();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
private void PrepareEntitiesBeforeSave()
{
foreach (var entry in ChangeTracker.Entries<Message>())
{
if (entry.State == EntityState.Added)
{
entry.Entity.CreatedTime = DateTime.UtcNow;
}
}
}
}
}