You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.4 KiB
90 lines
3.4 KiB
using System.Text.Json;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
using Microsoft.Extensions.Options;
|
|
using MyLibraryEntities;
|
|
|
|
namespace MyLibraryDB;
|
|
|
|
public class MyLibraryContext : DbContext
|
|
{
|
|
public DbSet<AuthorEntity> Authors { get; set; }
|
|
public DbSet<BookEntity> Books { get; set; }
|
|
public DbSet<WorkEntity> Works { get; set; }
|
|
|
|
public MyLibraryContext()
|
|
{ }
|
|
|
|
public MyLibraryContext(DbContextOptions<MyLibraryContext> options)
|
|
: base(options)
|
|
{ }
|
|
|
|
private static DbContextOptions<MyLibraryContext> InitPlaftormDB(string dbPlatformPath)
|
|
{
|
|
var options = new DbContextOptionsBuilder<MyLibraryContext>()
|
|
.UseMySql($"{dbPlatformPath}", new MySqlServerVersion(new Version(10, 11, 1)))
|
|
.Options;
|
|
|
|
return options;
|
|
}
|
|
|
|
public MyLibraryContext(string dbPlatformPath)
|
|
: this(InitPlaftormDB(dbPlatformPath))
|
|
{
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
optionsBuilder.UseSqlite($"Data Source=MylibraryDB.db");
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.Entity<AuthorEntity>()
|
|
.Property(e => e.AlternateNames)
|
|
.HasConversion(
|
|
v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
|
|
v => JsonSerializer.Deserialize<List<string>>(v, (JsonSerializerOptions)null),
|
|
new ValueComparer<ICollection<string>>(
|
|
(c1, c2) => c1.SequenceEqual(c2),
|
|
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
|
|
c => (ICollection<string>)c.ToList()));
|
|
|
|
modelBuilder.Entity<WorkEntity>()
|
|
.Property(e => e.Subjects)
|
|
.HasConversion(
|
|
v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
|
|
v => JsonSerializer.Deserialize<List<string>>(v, (JsonSerializerOptions)null),
|
|
new ValueComparer<ICollection<string>>(
|
|
(c1, c2) => c1.SequenceEqual(c2),
|
|
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
|
|
c => (ICollection<string>)c.ToList()));
|
|
|
|
modelBuilder.Entity<BookEntity>()
|
|
.Property(e => e.Publishers)
|
|
.HasConversion(
|
|
v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
|
|
v => JsonSerializer.Deserialize<List<string>>(v, (JsonSerializerOptions)null),
|
|
new ValueComparer<ICollection<string>>(
|
|
(c1, c2) => c1.SequenceEqual(c2),
|
|
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
|
|
c => (ICollection<string>)c.ToList()));
|
|
|
|
modelBuilder.Entity<BookEntity>()
|
|
.Property(e => e.Series)
|
|
.HasConversion(
|
|
v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
|
|
v => JsonSerializer.Deserialize<List<string>>(v, (JsonSerializerOptions)null),
|
|
new ValueComparer<ICollection<string>>(
|
|
(c1, c2) => c1.SequenceEqual(c2),
|
|
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
|
|
c => (ICollection<string>)c.ToList()));
|
|
}
|
|
}
|
|
|