2017-11-25 20:29:58 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.Entity;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SharedLibrary.Database.Models;
|
|
|
|
|
using System.Data.SqlServerCe;
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using System.Data.Entity.ModelConfiguration.Conventions;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
using System.Reflection;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
using System.Data.Entity.Infrastructure;
|
|
|
|
|
using System.Data.Entity.SqlServerCompact;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
using System.IO;
|
2018-04-06 20:15:17 -04:00
|
|
|
|
using System.Data.Common;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
|
|
|
|
namespace SharedLibrary.Database
|
|
|
|
|
{
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
2018-04-06 20:15:17 -04:00
|
|
|
|
[DbConfigurationType(typeof(ContextConfiguration))]
|
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-04-06 20:15:17 -04:00
|
|
|
|
public static string ConnectionString;
|
|
|
|
|
|
|
|
|
|
public DatabaseContext() : base(ConnectionString)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, Migrations.Configuration>());
|
|
|
|
|
//Database.CreateIfNotExists();
|
2018-02-10 01:26:38 -05:00
|
|
|
|
Configuration.LazyLoadingEnabled = true;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(DbModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
modelBuilder.Entity<EFPenalty>()
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.HasRequired(p => p.Offender)
|
|
|
|
|
.WithMany(c => c.ReceivedPenalties)
|
|
|
|
|
.HasForeignKey(c => c.OffenderId)
|
|
|
|
|
.WillCascadeOnDelete(false);
|
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
modelBuilder.Entity<EFPenalty>()
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.HasRequired(p => p.Punisher)
|
|
|
|
|
.WithMany(c => c.AdministeredPenalties)
|
|
|
|
|
.HasForeignKey(c => c.PunisherId)
|
|
|
|
|
.WillCascadeOnDelete(false);
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<EFAliasLink>()
|
|
|
|
|
.HasMany(e => e.Children)
|
|
|
|
|
.WithRequired(a => a.Link)
|
|
|
|
|
.HasForeignKey(a => a.LinkId)
|
|
|
|
|
.WillCascadeOnDelete(true);
|
|
|
|
|
|
2017-11-29 19:35:50 -05:00
|
|
|
|
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
|
|
|
|
|
|
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-03-06 02:22:19 -05:00
|
|
|
|
//string dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar;
|
|
|
|
|
#if !DEBUG
|
2018-02-21 20:29:23 -05:00
|
|
|
|
foreach (string dllPath in System.IO.Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins"))
|
2018-03-06 02:22:19 -05:00
|
|
|
|
#else
|
2018-04-05 00:38:45 -04:00
|
|
|
|
IEnumerable<string> directoryFiles;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-04-06 22:06:43 -04:00
|
|
|
|
directoryFiles = Directory.GetFiles($@"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}x86{Path.DirectorySeparatorChar}Debug{Path.DirectorySeparatorChar}Plugins").Where(f => f.Contains(".dll"));
|
2018-04-05 00:38:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 20:15:17 -04:00
|
|
|
|
catch (Exception)
|
2018-04-05 00:38:45 -04:00
|
|
|
|
{
|
2018-04-06 22:06:43 -04:00
|
|
|
|
directoryFiles = Directory.GetFiles($@"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}Plugins").Where(f => f.Contains(".dll"));
|
2018-04-05 00:38:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (string dllPath in directoryFiles)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
#endif
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
|
|
|
|
Assembly library;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
library = Assembly.LoadFile(dllPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// not a valid assembly, ie plugin files
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
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)))
|
|
|
|
|
{
|
|
|
|
|
var method = modelBuilder.GetType().GetMethod("Entity");
|
|
|
|
|
method = method.MakeGenericMethod(new Type[] { type });
|
|
|
|
|
method.Invoke(modelBuilder, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|