mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 07:13:58 -05:00
Moved from SQLITE to EntityFramework.
Lots of things are broken!
This commit is contained in:
49
SharedLibrary/Database/IW4MAdminDatabaseContext.cs
Normal file
49
SharedLibrary/Database/IW4MAdminDatabaseContext.cs
Normal file
@ -0,0 +1,49 @@
|
||||
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;
|
||||
|
||||
namespace SharedLibrary.Database
|
||||
{
|
||||
public class IW4MAdminDatabaseContext : 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 IW4MAdminDatabaseContext() : base("DefaultConnection")
|
||||
{
|
||||
System.Data.Entity.Database.SetInitializer(new Initializer());
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(DbModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<EFPenalty>()
|
||||
.HasRequired(p => p.Punisher)
|
||||
.WithMany(c => c.AdministeredPenalties)
|
||||
.HasForeignKey(c => c.PunisherId)
|
||||
.WillCascadeOnDelete(false);
|
||||
|
||||
modelBuilder.Entity<EFPenalty>()
|
||||
.HasRequired(p => p.Offender)
|
||||
.WithMany(c => c.ReceivedPenalties)
|
||||
.HasForeignKey(c => c.OffenderId)
|
||||
.WillCascadeOnDelete(false);
|
||||
|
||||
modelBuilder.Entity<EFAliasLink>()
|
||||
.HasMany(e => e.Children)
|
||||
.WithRequired(a => a.Link)
|
||||
.HasForeignKey(a => a.LinkId)
|
||||
.WillCascadeOnDelete(true);
|
||||
|
||||
// todo custom load DBSets from plugins
|
||||
// https://aleemkhan.wordpress.com/2013/02/28/dynamically-adding-dbset-properties-in-dbcontext-for-entity-framework-code-first/
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
}
|
||||
}
|
32
SharedLibrary/Database/Initializer.cs
Normal file
32
SharedLibrary/Database/Initializer.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Entity;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database
|
||||
{
|
||||
public class Initializer : DropCreateDatabaseIfModelChanges<IW4MAdminDatabaseContext>
|
||||
{
|
||||
protected override void Seed(IW4MAdminDatabaseContext context)
|
||||
{
|
||||
context.Clients.Add(new Models.EFClient()
|
||||
{
|
||||
Active = false,
|
||||
Connections = 0,
|
||||
AliasLink = new Models.EFAliasLink(),
|
||||
FirstConnection = DateTime.UtcNow,
|
||||
IPAddress = "127.0.0.1",
|
||||
LastConnection = DateTime.UtcNow,
|
||||
Level = Objects.Player.Permission.Console,
|
||||
Name = "IW4MAdmin",
|
||||
Masked = true,
|
||||
NetworkId = "0000000000000000",
|
||||
});
|
||||
|
||||
base.Seed(context);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
26
SharedLibrary/Database/Models/EFAlias.cs
Normal file
26
SharedLibrary/Database/Models/EFAlias.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database.Models
|
||||
{
|
||||
public class EFAlias : SharedEntity
|
||||
{
|
||||
[Key]
|
||||
public int AliasId { get; set; }
|
||||
[Required]
|
||||
public int LinkId { get; set; }
|
||||
[ForeignKey("LinkId")]
|
||||
public virtual EFAliasLink Link { get; set; }
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
[Required]
|
||||
public string IP { get; set; }
|
||||
[Required]
|
||||
public DateTime DateAdded { get; set; }
|
||||
}
|
||||
}
|
17
SharedLibrary/Database/Models/EFAliasLink.cs
Normal file
17
SharedLibrary/Database/Models/EFAliasLink.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SharedLibrary.Database.Models
|
||||
{
|
||||
public class EFAliasLink : SharedEntity
|
||||
{
|
||||
[Key]
|
||||
public int AliasLinkId { get; set; }
|
||||
public virtual ICollection<EFAlias> Children { get; set; }
|
||||
|
||||
public EFAliasLink()
|
||||
{
|
||||
Children = new List<EFAlias>();
|
||||
}
|
||||
}
|
||||
}
|
46
SharedLibrary/Database/Models/EFClient.cs
Normal file
46
SharedLibrary/Database/Models/EFClient.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database.Models
|
||||
{
|
||||
public class EFClient : SharedEntity
|
||||
{
|
||||
[Key]
|
||||
public int ClientId { get; set; }
|
||||
[Index(IsUnique = true)]
|
||||
public string NetworkId { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
[Required]
|
||||
public Objects.Player.Permission Level { get; set; }
|
||||
[Required]
|
||||
public int Connections { get; set; }
|
||||
[Required]
|
||||
public int TotalConnectionTime { get; set; }
|
||||
[Required]
|
||||
public string IPAddress { get; set; }
|
||||
[Required]
|
||||
public DateTime FirstConnection { get; set; }
|
||||
[Required]
|
||||
public DateTime LastConnection { get; set; }
|
||||
public bool Masked { get; set; }
|
||||
[Required]
|
||||
public int AliasLinkId { get; set; }
|
||||
[ForeignKey("AliasLinkId")]
|
||||
public virtual EFAliasLink AliasLink { get; set; }
|
||||
public virtual ICollection<EFPenalty> ReceivedPenalties { get; set; }
|
||||
public virtual ICollection<EFPenalty> AdministeredPenalties { get; set; }
|
||||
|
||||
public EFClient()
|
||||
{
|
||||
ReceivedPenalties = new List<EFPenalty>();
|
||||
AdministeredPenalties = new List<EFPenalty>();
|
||||
}
|
||||
}
|
||||
}
|
35
SharedLibrary/Database/Models/EFPenalty.cs
Normal file
35
SharedLibrary/Database/Models/EFPenalty.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database.Models
|
||||
{
|
||||
public class EFPenalty : SharedEntity
|
||||
{
|
||||
[Key]
|
||||
public int PenaltyId { get; set; }
|
||||
[Required]
|
||||
public int LinkId { get; set; }
|
||||
[ForeignKey("LinkId")]
|
||||
public virtual EFAliasLink Link { get; set; }
|
||||
[Required]
|
||||
public int OffenderId { get; set; }
|
||||
[ForeignKey("OffenderId")]
|
||||
public virtual EFClient Offender { get; set; }
|
||||
[Required]
|
||||
public int PunisherId { get; set; }
|
||||
[ForeignKey("PunisherId")]
|
||||
public virtual EFClient Punisher { get; set; }
|
||||
[Required]
|
||||
public DateTime When { get; set; }
|
||||
[Required]
|
||||
public DateTime Expires { get; set; }
|
||||
[Required]
|
||||
public string Offense { get; set; }
|
||||
public Objects.Penalty.PenaltyType Type { get; set; }
|
||||
}
|
||||
}
|
13
SharedLibrary/Database/Models/SharedEntity.cs
Normal file
13
SharedLibrary/Database/Models/SharedEntity.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database.Models
|
||||
{
|
||||
public class SharedEntity
|
||||
{
|
||||
public bool Active { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user