IW4M-Admin/SharedLibraryCore/Database/DatabaseContext.cs

149 lines
5.5 KiB
C#
Raw Normal View History

using System;
2018-04-08 02:44:42 -04:00
using Microsoft.EntityFrameworkCore;
using SharedLibraryCore.Database.Models;
using System.Reflection;
2018-03-06 02:22:19 -05:00
using System.IO;
2018-04-08 02:44:42 -04:00
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.Sqlite;
using SharedLibraryCore.Interfaces;
using System.Runtime.InteropServices;
2018-04-08 02:44:42 -04:00
namespace SharedLibraryCore.Database
{
2017-11-29 19:35:50 -05:00
public class DatabaseContext : DbContext
{
public DbSet<EFClient> Clients { get; set; }
public DbSet<EFAlias> Aliases { get; set; }
public DbSet<EFAliasLink> AliasLinks { get; set; }
public DbSet<EFPenalty> Penalties { get; set; }
public DbSet<EFMeta> EFMeta { get; set; }
public DbSet<EFChangeHistory> EFChangeHistory { get; set; }
private static string _ConnectionString;
2018-04-08 02:44:42 -04:00
public DatabaseContext(DbContextOptions<DatabaseContext> opt) : base(opt) { }
public DatabaseContext(string connStr)
{
_ConnectionString = connStr;
}
2018-04-08 02:44:42 -04:00
public DatabaseContext()
{
}
2018-04-08 02:44:42 -04:00
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (string.IsNullOrEmpty(_ConnectionString))
{
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
// allows the application to find the database file
currentPath = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
$"{Path.DirectorySeparatorChar}{currentPath}" :
currentPath;
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = $"{currentPath}{Path.DirectorySeparatorChar}Database.db".Substring(6) };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
2018-04-08 02:44:42 -04:00
optionsBuilder.UseSqlite(connection);
}
else
{
optionsBuilder.UseMySql(_ConnectionString);
}
2018-04-08 02:44:42 -04:00
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// make network id unique
modelBuilder.Entity<EFClient>(entity =>
{
entity.HasIndex(e => e.NetworkId).IsUnique();
});
modelBuilder.Entity<EFPenalty>(entity =>
{
entity.HasOne(p => p.Offender)
2017-11-29 19:35:50 -05:00
.WithMany(c => c.ReceivedPenalties)
.HasForeignKey(c => c.OffenderId)
2018-04-08 02:44:42 -04:00
.OnDelete(DeleteBehavior.Restrict);
2017-11-29 19:35:50 -05:00
2018-04-08 02:44:42 -04:00
entity.HasOne(p => p.Punisher)
.WithMany(p => p.AdministeredPenalties)
2017-11-29 19:35:50 -05:00
.HasForeignKey(c => c.PunisherId)
2018-04-08 02:44:42 -04:00
.OnDelete(DeleteBehavior.Restrict);
});
2018-04-08 02:44:42 -04:00
modelBuilder.Entity<EFAliasLink>(entity =>
{
entity.HasMany(e => e.Children)
.WithOne(a => a.Link)
.HasForeignKey(k => k.LinkId)
.OnDelete(DeleteBehavior.Restrict);
});
2017-11-29 19:35:50 -05:00
modelBuilder.Entity<EFAlias>(ent =>
{
ent.HasIndex(a => a.IPAddress);
});
// force full name for database conversion
modelBuilder.Entity<EFClient>().ToTable("EFClients");
modelBuilder.Entity<EFAlias>().ToTable("EFAlias");
modelBuilder.Entity<EFAliasLink>().ToTable("EFAliasLinks");
modelBuilder.Entity<EFPenalty>().ToTable("EFPenalties");
// https://aleemkhan.wordpress.com/2013/02/28/dynamically-adding-dbset-properties-in-dbcontext-for-entity-framework-code-first/
2018-03-06 02:22:19 -05:00
#if !DEBUG
2018-04-08 02:44:42 -04:00
foreach (string dllPath in Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins"))
2018-03-06 02:22:19 -05:00
#else
IEnumerable<string> directoryFiles;
try
{
2018-04-08 02:44:42 -04:00
directoryFiles = Directory.GetFiles($@"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}Debug{Path.DirectorySeparatorChar}netcoreapp2.0{Path.DirectorySeparatorChar}Plugins").Where(f => f.Contains(".dll"));
}
catch (Exception)
{
directoryFiles = Directory.GetFiles($@"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}Plugins").Where(f => f.Contains(".dll"));
}
foreach (string dllPath in directoryFiles)
2018-03-06 02:22:19 -05:00
#endif
{
Assembly library;
try
{
library = Assembly.LoadFrom(dllPath);
}
// not a valid assembly, ie plugin support files
catch (Exception)
{
continue;
}
var configurations = library.ExportedTypes.Where(c => c.GetInterfaces().FirstOrDefault(i => typeof(IModelConfiguration).IsAssignableFrom(i)) != null)
.Select(c => (IModelConfiguration)Activator.CreateInstance(c));
foreach (var configurable in configurations)
configurable.Configure(modelBuilder);
2018-03-06 02:22:19 -05:00
foreach (var type in library.ExportedTypes)
{
if (type.IsClass && type.IsSubclassOf(typeof(SharedEntity)))
{
var method = modelBuilder.GetType().GetMethod("Entity", new[] { typeof(Type) });
method.Invoke(modelBuilder, new[] { type });
}
}
}
base.OnModelCreating(modelBuilder);
}
}
}