Add project files.
This commit is contained in:
71
Data/ApplicationDbContext.cs
Normal file
71
Data/ApplicationDbContext.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using LogisticsApp.Server.Models;
|
||||
|
||||
namespace LogisticsApp.Server.Data
|
||||
{
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
|
||||
|
||||
public DbSet<Order> Orders { get; set; }
|
||||
public DbSet<Vehicle> Vehicles { get; set; }
|
||||
public DbSet<WaybillEntry> WaybillEntries { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
||||
{
|
||||
foreach (var property in entityType.GetProperties())
|
||||
{
|
||||
if (property.ClrType == typeof(DateTime))
|
||||
{
|
||||
modelBuilder.Entity(entityType.ClrType)
|
||||
.Property<DateTime>(property.Name)
|
||||
.HasConversion(
|
||||
v => v.Kind == DateTimeKind.Unspecified
|
||||
? DateTime.SpecifyKind(v, DateTimeKind.Utc)
|
||||
: v.ToUniversalTime(),
|
||||
v => DateTime.SpecifyKind(v, DateTimeKind.Utc));
|
||||
}
|
||||
else if (property.ClrType == typeof(DateTime?))
|
||||
{
|
||||
modelBuilder.Entity(entityType.ClrType)
|
||||
.Property<DateTime?>(property.Name)
|
||||
.HasConversion(
|
||||
v => !v.HasValue
|
||||
? v
|
||||
: (v.Value.Kind == DateTimeKind.Unspecified
|
||||
? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc)
|
||||
: v.Value.ToUniversalTime()),
|
||||
v => v.HasValue
|
||||
? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc)
|
||||
: v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
modelBuilder.Entity<Order>()
|
||||
.HasIndex(o => o.Status);
|
||||
|
||||
modelBuilder.Entity<Order>()
|
||||
.HasIndex(o => o.OrderDate);
|
||||
|
||||
modelBuilder.Entity<Order>()
|
||||
.HasIndex(o => o.ClientName);
|
||||
|
||||
modelBuilder.Entity<Vehicle>()
|
||||
.HasIndex(v => v.LicensePlate);
|
||||
|
||||
modelBuilder.Entity<WaybillEntry>()
|
||||
.HasIndex(w => new { w.VehicleId, w.Date });
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.HasIndex(u => u.Username)
|
||||
.IsUnique();
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user