NorthwindDbContext : DbContext, INorthwindDbContext
Fri Apr 22 2022 20:11:24 GMT+0000 (Coordinated Universal Time)
Saved by @iamsingularity #csharp
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Northwind.Application.Common.Interfaces;
using Northwind.Common;
using Northwind.Domain.Entities;
using Northwind.Domain.Common;
namespace Northwind.Persistence
{
public class NorthwindDbContext : DbContext, INorthwindDbContext
{
private readonly ICurrentUserService _currentUserService;
private readonly IDateTime _dateTime;
public NorthwindDbContext(DbContextOptions<NorthwindDbContext> options)
: base(options)
{
}
public NorthwindDbContext(
DbContextOptions<NorthwindDbContext> options,
ICurrentUserService currentUserService,
IDateTime dateTime)
: base(options)
{
_currentUserService = currentUserService;
_dateTime = dateTime;
}
public DbSet<Category> Categories { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<EmployeeTerritory> EmployeeTerritories { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Region> Region { get; set; }
public DbSet<Shipper> Shippers { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<Territory> Territories { get; set; }
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
foreach (var entry in ChangeTracker.Entries<AuditableEntity>())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.CreatedBy = _currentUserService.UserId;
entry.Entity.Created = _dateTime.Now;
break;
case EntityState.Modified:
entry.Entity.LastModifiedBy = _currentUserService.UserId;
entry.Entity.LastModified = _dateTime.Now;
break;
}
}
return base.SaveChangesAsync(cancellationToken);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(NorthwindDbContext).Assembly);
}
}
}



Comments