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;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
|
|
|
|
namespace SharedLibrary.Database
|
|
|
|
|
{
|
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; }
|
|
|
|
|
|
2017-11-29 19:35:50 -05:00
|
|
|
|
public DatabaseContext() : base("DefaultConnection")
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
System.Data.Entity.Database.SetInitializer(new Initializer());
|
2017-11-29 19:35:50 -05:00
|
|
|
|
Configuration.LazyLoadingEnabled = false;
|
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.Entity<EFAlias>()
|
|
|
|
|
.HasIndex(a => new { a.IP, a.Name })
|
|
|
|
|
.IsUnique();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<EFAlias>()
|
|
|
|
|
.HasIndex(p => new { p.Name, p.IPAddress }).IsUnique();*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
|
|
|
|
|
|
|
|
|
|
// todo: custom load DBSets from plugins
|
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/
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|