2017-11-25 20:29:58 -05:00
|
|
|
|
using System;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
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;
|
2018-04-08 17:50:58 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-08-01 22:09:22 -04:00
|
|
|
|
using System.Runtime.InteropServices;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.Database
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
public class DatabaseContext : DbContext
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
public DbSet<EFClient> Clients { get; set; }
|
|
|
|
|
public DbSet<EFAlias> Aliases { get; set; }
|
|
|
|
|
public DbSet<EFAliasLink> AliasLinks { get; set; }
|
|
|
|
|
public DbSet<EFPenalty> Penalties { get; set; }
|
2018-06-02 00:48:10 -04:00
|
|
|
|
public DbSet<EFMeta> EFMeta { get; set; }
|
2018-06-16 22:11:25 -04:00
|
|
|
|
public DbSet<EFChangeHistory> EFChangeHistory { get; set; }
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// this only works if there's one connection string
|
|
|
|
|
/// </summary>
|
2018-04-25 02:38:59 -04:00
|
|
|
|
private static string _ConnectionString;
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
public DatabaseContext(DbContextOptions<DatabaseContext> opt) : base(opt) { }
|
2018-04-06 20:15:17 -04:00
|
|
|
|
|
2018-08-03 18:10:20 -04:00
|
|
|
|
public DatabaseContext() { }
|
|
|
|
|
|
2018-04-25 02:38:59 -04:00
|
|
|
|
public DatabaseContext(string connStr)
|
|
|
|
|
{
|
|
|
|
|
_ConnectionString = connStr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2018-04-25 02:38:59 -04:00
|
|
|
|
if (string.IsNullOrEmpty(_ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
|
2018-08-01 22:09:22 -04:00
|
|
|
|
// allows the application to find the database file
|
|
|
|
|
currentPath = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
|
|
|
|
|
$"{Path.DirectorySeparatorChar}{currentPath}" :
|
|
|
|
|
currentPath;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2018-04-25 02:38:59 -04:00
|
|
|
|
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
|
|
|
|
|
2018-04-25 02:38:59 -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);
|
|
|
|
|
});
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
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
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
modelBuilder.Entity<EFAlias>(ent =>
|
|
|
|
|
{
|
|
|
|
|
ent.HasIndex(a => a.IPAddress);
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-09 15:17:10 -04:00
|
|
|
|
// 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");
|
|
|
|
|
|
2018-08-02 21:52:35 -04:00
|
|
|
|
// adapted from
|
2017-11-25 20:29:58 -05:00
|
|
|
|
// https://aleemkhan.wordpress.com/2013/02/28/dynamically-adding-dbset-properties-in-dbcontext-for-entity-framework-code-first/
|
2018-04-05 00:38:45 -04:00
|
|
|
|
IEnumerable<string> directoryFiles;
|
|
|
|
|
|
2018-08-28 17:32:59 -04:00
|
|
|
|
string pluginDir = $@"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}Debug{Path.DirectorySeparatorChar}netcoreapp2.0{Path.DirectorySeparatorChar}Plugins";
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(pluginDir))
|
2018-04-05 00:38:45 -04:00
|
|
|
|
{
|
2018-08-28 17:32:59 -04:00
|
|
|
|
pluginDir = $@"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}Plugins";
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(pluginDir))
|
|
|
|
|
{
|
|
|
|
|
pluginDir = Utilities.OperatingDirectory;
|
|
|
|
|
}
|
2018-04-05 00:38:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-28 17:32:59 -04:00
|
|
|
|
directoryFiles = Directory.GetFiles(pluginDir).Where(f => f.Contains(".dll"));
|
2018-09-02 17:59:27 -04:00
|
|
|
|
#if DEBUG == TRUE
|
|
|
|
|
foreach (string dllPath in Directory.GetFiles(@"C:\Projects\IW4M-Admin\Application\bin\Debug\netcoreapp2.1\Plugins"))
|
|
|
|
|
#else
|
|
|
|
|
foreach (string dllPath in directoryFiles)
|
|
|
|
|
#endif
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
|
|
|
|
Assembly library;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-04-08 17:50:58 -04:00
|
|
|
|
library = Assembly.LoadFrom(dllPath);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 01:48:14 -04:00
|
|
|
|
// not a valid assembly, ie plugin support files
|
2018-02-07 00:19:06 -05:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-04-25 02:38:59 -04:00
|
|
|
|
|
2018-04-08 17:50:58 -04:00
|
|
|
|
var configurations = library.ExportedTypes.Where(c => c.GetInterfaces().FirstOrDefault(i => typeof(IModelConfiguration).IsAssignableFrom(i)) != null)
|
2018-04-25 02:38:59 -04:00
|
|
|
|
.Select(c => (IModelConfiguration)Activator.CreateInstance(c));
|
2018-04-08 17:50:58 -04:00
|
|
|
|
|
|
|
|
|
foreach (var configurable in configurations)
|
|
|
|
|
configurable.Configure(modelBuilder);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
foreach (var type in library.ExportedTypes)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
|
|
|
|
if (type.IsClass && type.IsSubclassOf(typeof(SharedEntity)))
|
|
|
|
|
{
|
2018-04-08 17:50:58 -04:00
|
|
|
|
var method = modelBuilder.GetType().GetMethod("Entity", new[] { typeof(Type) });
|
|
|
|
|
method.Invoke(modelBuilder, new[] { type });
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|