Stuff and things

This commit is contained in:
2021-06-29 19:00:13 -04:00
parent 0ff42f148a
commit 962ad3383f
22 changed files with 320 additions and 226 deletions

View File

@@ -0,0 +1,84 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using PartSource.Data.Converters;
using PartSource.Data.Models;
namespace PartSource.Data.Contexts
{
public class PartSourceContext : DbContext
{
public PartSourceContext(DbContextOptions<PartSourceContext> contextOptions)
: base(contextOptions) { }
public DbSet<ApiClient> ApiClients { get; set; }
public DbSet<ProductBackup> ProductBackups { get; set; }
public DbSet<Manufacturer> Manufacturers { get; set; }
public DbSet<ImportData> ImportData { get; set; }
public DbSet<ImportMetric> ImportMetrics { get; set; }
public DbSet<PartData> PartData { get; set; }
public DbSet<PartImage> PartImages { get; set; }
public DbSet<PartPrice> PartPrices { get; set; }
public DbSet<Part> Parts { get; set; }
public DbSet<ShopifyChangelog> ShopifyChangelogs { get; set; }
public DbSet<Vehicle> Vehicles { get; set; }
public DbSet<PartsAvailability> PartAvailabilities { get; set; }
public DbQuery<BaseVehicle> BaseVehicles { get; set; }
public DbQuery<Engine> Engines { get; set; }
public DbQuery<Submodel> Submodels { get; set; }
public DbQuery<VehicleMake> VehicleMakes { get; set; }
public DbQuery<VehicleModel> VehicleModels { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Query<BaseVehicle>().ToView(nameof(BaseVehicle));
modelBuilder.Query<Engine>().ToView(nameof(Engine));
modelBuilder.Query<Submodel>().ToView(nameof(Submodel));
modelBuilder.Query<VehicleMake>().ToView(nameof(VehicleMake));
modelBuilder.Query<VehicleModel>().ToView(nameof(VehicleModel));
modelBuilder.Entity<PartsAvailability>().HasKey(p => new { p.Store, p.SKU });
modelBuilder.Entity<ShopifyChangelog>()
.Property(s => s.ResourceType)
.HasConversion(new TypeToStringConverter());
modelBuilder.Entity<ShopifyChangelog>()
.Property(s => s.Data)
.HasConversion(new ObjectToJsonConverter());
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
entityType.Relational().TableName = entityType.ClrType.Name;
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=localhost;Database=ps-whi-stage;Trusted_Connection=True;");
}
}
}
}