Lots of data and model setup
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
8
DCGEngine.Database/Interfaces/IDataSeeder.cs
Normal file
8
DCGEngine.Database/Interfaces/IDataSeeder.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DCGEngine.Database.Interfaces;
|
||||
|
||||
public interface IDataSeeder
|
||||
{
|
||||
void Seed(ModelBuilder builder);
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user