Lots of data and model setup

This commit is contained in:
gamer147
2025-05-18 02:27:17 -04:00
parent 79505e0c1a
commit b2024af852
77 changed files with 81988 additions and 433 deletions

View File

@@ -18,13 +18,13 @@ public class DCGEDatabaseConfiguration
#endregion
#region Manual Configuration
/// <summary>
/// Assemblies to be searched for classes implementing <see cref="BaseEntity{TKey}"/> to be added as <see cref="DbSet{TEntity}"/>s. Should be set in code, not in appsettings.
/// </summary>
public List<Assembly> DbSetSearchAssemblies { get; set; }
public List<Assembly> DbSetSearchAssemblies { get; set; } = new List<Assembly>();
#endregion
}

View File

@@ -26,7 +26,7 @@ public class DCGEDbContext : DbContext
{
if (entityEntry.Entity is ITimeTrackedEntity timeTrackedEntity)
{
if (entityEntry.State is EntityState.Added && timeTrackedEntity.DateCreated is null)
if (entityEntry.State is EntityState.Added && timeTrackedEntity.DateCreated == DateTime.MinValue)
{
timeTrackedEntity.DateCreated = DateTime.UtcNow;
}
@@ -49,7 +49,15 @@ public class DCGEDbContext : DbContext
{
modelBuilder.Entity(typeInfo.AsType());
}
foreach (var typeInfo in assembly.DefinedTypes.Where(type => type.IsAssignableTo(typeof(IDataSeeder))))
{
((IDataSeeder?)Activator.CreateInstance(typeInfo.AsType()))?.Seed(modelBuilder);
}
});
modelBuilder.Entity<DeckEntry>()
.OwnsMany<DeckCard>(de => de.Cards);
base.OnModelCreating(modelBuilder);
}

View File

@@ -0,0 +1,8 @@
using Microsoft.EntityFrameworkCore;
namespace DCGEngine.Database.Interfaces;
public interface IDataSeeder
{
void Seed(ModelBuilder builder);
}

View File

@@ -5,7 +5,7 @@ public interface ITimeTrackedEntity
/// <summary>
/// The <see cref="DateTime"/> this entity was first added to the database.
/// </summary>
public DateTime? DateCreated { get; set; }
public DateTime DateCreated { get; set; }
/// <summary>
/// The <see cref="DateTime"/> this entity was last updated.

View File

@@ -10,7 +10,7 @@ public class BaseEntity<TKey> : ITimeTrackedEntity, IDbTrackedEntity
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual TKey Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime DateCreated { get; set; } = DateTime.MinValue;
public DateTime? DateUpdated { get; set; }
}

View File

@@ -1,11 +1,13 @@
using System.ComponentModel.DataAnnotations;
using DCGEngine.Database.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace DCGEngine.Database.Models;
/// <summary>
/// A deck consisting of multiple <see cref="CardEntry"/> stored in the DB.
/// </summary>
public abstract class DeckEntry : BaseEntity<long>
public abstract class DeckEntry : BaseEntity<Guid>
{
/// <summary>
/// How this deck is referred to internally.