-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppDbContext.cs
executable file
·35 lines (28 loc) · 1.05 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
namespace Money_CLI;
using Microsoft.EntityFrameworkCore;
using Money_CLI.Models;
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
public class AppDbContext : DbContext
{
public DbSet<Expense> Expenses { get; set; }
public DbSet<Income> Incomes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(@$"Data Source={SystemVariables.DatabaseFolder}money.db");
base.OnConfiguring(optionsBuilder);
}
/// <summary>
/// <inheritdoc />
/// <br />
/// Overriden to set the default values for the <paramref name="DateIn" /> on creation automatically.
/// </summary>
public override int SaveChanges()
{
var entries = ChangeTracker
.Entries()
.Where(e => e.Entity is ChangeBase && e.State == EntityState.Added);
foreach (var entityEntry in entries)
((ChangeBase)entityEntry.Entity).DateIn = DateTime.Now;
return base.SaveChanges();
}
}