-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathMoryxDbContext.cs
More file actions
88 lines (73 loc) · 2.89 KB
/
Copy pathMoryxDbContext.cs
File metadata and controls
88 lines (73 loc) · 2.89 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2026 Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Moryx.Model.Annotations;
using Moryx.Model.Attributes;
namespace Moryx.Model;
/// <summary>
/// Moryx related implementation of <see cref="T:Microsoft.EntityFrameworkCore.DbContext" />
/// </summary>
public abstract class MoryxDbContext : DbContext
{
/// <inheritdoc />
protected MoryxDbContext()
{
}
/// <inheritdoc />
protected MoryxDbContext(DbContextOptions options) : base(options)
{
}
/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Utc DateTime conversion
modelBuilder.ApplyDateTimeKindConverter();
// Set default schema
var attributes = GetType().GetCustomAttributes<DefaultSchemaAttribute>().ToArray();
var defaultSchemaAttr = attributes.LastOrDefault();
modelBuilder.HasDefaultSchema(!string.IsNullOrEmpty(defaultSchemaAttr?.Schema)
? defaultSchemaAttr.Schema.ToLower() // schema names have to be lower case!
: DefaultSchemaAttribute.DefaultName);
}
/// <inheritdoc />
public override int SaveChanges()
{
ApplyModificationTracking();
return base.SaveChanges();
}
/// <inheritdoc />
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
ApplyModificationTracking();
return base.SaveChangesAsync(cancellationToken);
}
/// <inheritdoc />
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
ApplyModificationTracking();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
/// <summary>
/// Applies the modification tracking and updates the Created, Updated and Deleted columns
/// </summary>
private void ApplyModificationTracking()
{
var modifiedTrackedEntries = ChangeTracker.Entries()
.Where(entry => entry.Entity is IModificationTrackedEntity &&
(entry.State == EntityState.Added || entry.State == EntityState.Modified));
var timeStamp = DateTime.UtcNow;
foreach (var entry in modifiedTrackedEntries)
{
var entity = (IModificationTrackedEntity)entry.Entity;
// Added gets created
if (entry.State == EntityState.Added)
entity.Created = timeStamp;
// All states gets updated
entity.Updated = timeStamp;
// Override deleted date of entity to sync between updated and deleted
if (entry.State == EntityState.Modified && entry.OriginalValues[nameof(IModificationTrackedEntity.Deleted)] == null && entity.Deleted != null)
entity.Deleted = timeStamp;
}
}
}