-
Notifications
You must be signed in to change notification settings - Fork 7
/
AppDbContext.cs
66 lines (57 loc) · 2.33 KB
/
AppDbContext.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TicTacToeCSharpPlayground.Core.Models;
using TicTacToeCSharpPlayground.Infrastructure.Database.FluentSetup;
namespace TicTacToeCSharpPlayground.Infrastructure.Database
{
public class AppDbContext : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Board> Boards { get; set; }
public DbSet<Game> Games { get; set; }
public DbSet<Movement> Movements { get; set; }
public DbSet<PlayerBoard> PlayerBoards { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
// Bodyless constructor
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// More details about the EF Fluent API in the following links:
// https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships#configuring-a-many-to-many-relationship
// https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/types-and-properties
FluentPlayer.Setup(modelBuilder);
FluentBoard.Setup(modelBuilder);
FluentPlayerBoard.Setup(modelBuilder);
FluentMovement.Setup(modelBuilder);
}
public override int SaveChanges()
{
AutomaticallyAddCreatedAndUpdatedAt();
return base.SaveChanges();
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
AutomaticallyAddCreatedAndUpdatedAt();
return base.SaveChangesAsync(cancellationToken);
}
private void AutomaticallyAddCreatedAndUpdatedAt()
{
var entitiesOnDbContext = ChangeTracker.Entries<StandardEntity>();
if (entitiesOnDbContext is null)
return;
foreach (var item in entitiesOnDbContext.Where(t => t.State == EntityState.Added))
{
item.Entity.CreatedAt = DateTime.Now;
item.Entity.UpdatedAt = DateTime.Now;
}
foreach (var item in entitiesOnDbContext.Where(t => t.State == EntityState.Modified))
{
item.Entity.UpdatedAt = DateTime.Now;
}
}
}
}