This commit is contained in:
2025-05-25 18:36:59 +02:00
parent e86a49d5f6
commit 90d7281050
29 changed files with 321 additions and 959 deletions

33
SQLiteEF/Context.cs Normal file
View File

@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace SQLiteEF;
public class Context(IConfiguration configuration) : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Game> Games { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(configuration.GetConnectionString("DefaultConnection"));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Player>()
.HasMany<Game>(p => p.Games)
.WithMany(g => g.PlayedBy);
modelBuilder.Entity<Player>()
.Navigation(p => p.Games)
.AutoInclude();
modelBuilder.Entity<TrackedTime>()
.HasOne<Player>(p => p.Player)
.WithMany(p => p.TrackedTimes)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<TrackedTime>()
.HasOne<Game>(p => p.Game)
.WithMany(g => g.TrackedTimes)
.OnDelete(DeleteBehavior.Cascade);
}
}

12
SQLiteEF/Game.cs Normal file
View File

@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
namespace SQLiteEF;
[PrimaryKey("AppId")]
public class Game(ulong appId, string name)
{
public ulong AppId { get; init; } = appId;
public string Name { get; init; } = name;
public ICollection<Player> PlayedBy { get; init; } = null!;
public ICollection<TrackedTime> TrackedTimes { get; init; } = null!;
}

6
SQLiteEF/IUpdateable.cs Normal file
View File

@ -0,0 +1,6 @@
namespace SQLiteEF;
public interface IUpdateable
{
public DateTime UpdatedAt { get; set; }
}

29
SQLiteEF/TrackedTime.cs Normal file
View File

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
namespace SQLiteEF;
[PrimaryKey("TimeStamp")]
public class TrackedTime
{
public Game Game { get; init; }
public Player Player { get; init; }
public DateTime TimeStamp { get; init; }
public ulong TimePlayed { get; init; }
public TrackedTime(Game game, Player player, ulong timePlayed, DateTime? timeStamp = null)
{
this.Game = game;
this.Player = player;
this.TimeStamp = timeStamp??DateTime.Now;
this.TimePlayed = timePlayed;
}
/// <summary>
/// EF CORE
/// </summary>
internal TrackedTime(ulong timePlayed, DateTime timeStamp)
{
this.TimePlayed = timePlayed;
this.TimeStamp = timeStamp;
}
}