Stuff and things
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using PartSource.Data.Converters;
|
||||
using PartSource.Data.Models;
|
||||
|
||||
namespace PartSource.Data
|
||||
namespace PartSource.Data.Contexts
|
||||
{
|
||||
public class PartSourceContext : DbContext
|
||||
{
|
||||
@@ -29,6 +30,8 @@ namespace PartSource.Data
|
||||
|
||||
public DbSet<Part> Parts { get; set; }
|
||||
|
||||
public DbSet<ShopifyChangelog> ShopifyChangelogs { get; set; }
|
||||
|
||||
public DbSet<Vehicle> Vehicles { get; set; }
|
||||
|
||||
public DbSet<PartsAvailability> PartAvailabilities { get; set; }
|
||||
@@ -55,11 +58,18 @@ namespace PartSource.Data
|
||||
|
||||
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)
|
||||
27
PartSource.Data/Converters/ObjectToJsonConverter.cs
Normal file
27
PartSource.Data/Converters/ObjectToJsonConverter.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace PartSource.Data.Converters
|
||||
{
|
||||
public class ObjectToJsonConverter : ValueConverter<object, string>
|
||||
{
|
||||
private static readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings()
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
ContractResolver = new DefaultContractResolver()
|
||||
{
|
||||
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||
}
|
||||
};
|
||||
|
||||
public ObjectToJsonConverter() : base(ObjectToJson, JsonToObject) { }
|
||||
|
||||
public static Expression<Func<string, object>> JsonToObject = value => JsonConvert.DeserializeObject(value, _serializerSettings);
|
||||
public static Expression<Func<object, string>> ObjectToJson = value => JsonConvert.SerializeObject(value, _serializerSettings);
|
||||
}
|
||||
}
|
||||
16
PartSource.Data/Converters/TypeToStringConverter.cs
Normal file
16
PartSource.Data/Converters/TypeToStringConverter.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace PartSource.Data.Converters
|
||||
{
|
||||
public class TypeToStringConverter : ValueConverter<Type, string>
|
||||
{
|
||||
public TypeToStringConverter() : base(TypeToString, StringToType) { }
|
||||
|
||||
public static Expression<Func<string, Type>> StringToType = value => Type.GetType(value);
|
||||
public static Expression<Func<Type, string>> TypeToString = value => value.AssemblyQualifiedName;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Contexts;
|
||||
using System;
|
||||
|
||||
namespace PartSource.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(PartSourceContext))]
|
||||
[DbContext(typeof(PartSourceContext))]
|
||||
[Migration("20190811020941_ImportDateDcfMapping")]
|
||||
partial class ImportDateDcfMapping
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Contexts;
|
||||
|
||||
namespace PartSource.Data.Migrations
|
||||
{
|
||||
|
||||
19
PartSource.Data/Models/ShopifyChangelog.cs
Normal file
19
PartSource.Data/Models/ShopifyChangelog.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace PartSource.Data.Models
|
||||
{
|
||||
public class ShopifyChangelog
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public Type ResourceType { get; set; }
|
||||
|
||||
public long ShopifyId { get; set; }
|
||||
|
||||
public DateTime Timestamp { get; set; }
|
||||
|
||||
public object Data { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user