This commit is contained in:
2025-05-26 01:44:26 +02:00
parent ebddd3c3ed
commit 1a08f932af
23 changed files with 526 additions and 256 deletions

View File

@ -1,16 +1,23 @@
using log4net;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace SQLiteEF;
public class Context(IConfiguration configuration) : DbContext
public class Context(DbContextOptions<Context> contextOptions) : DbContext(contextOptions)
{
public DbSet<Player> Players { get; set; }
public DbSet<Game> Games { get; set; }
private ILog Log { get; } = LogManager.GetLogger(typeof(Context));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(configuration.GetConnectionString("DefaultConnection"));
optionsBuilder.EnableDetailedErrors().EnableSensitiveDataLogging();
optionsBuilder.LogTo(s => Log.Debug(s), (_, level) => level <= LogLevel.Debug);
optionsBuilder.LogTo(s => Log.Info(s), (_, level) => level == LogLevel.Information);
optionsBuilder.LogTo(s => Log.Warn(s), (_, level) => level == LogLevel.Warning);
optionsBuilder.LogTo(s => Log.Error(s), (_, level) => level == LogLevel.Error);
optionsBuilder.LogTo(s => Log.Fatal(s), (_, level) => level >= LogLevel.Critical);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
@ -20,14 +27,29 @@ public class Context(IConfiguration configuration) : DbContext
.WithMany(g => g.PlayedBy);
modelBuilder.Entity<Player>()
.Navigation(p => p.Games)
.AutoInclude();
.AutoInclude(false);
modelBuilder.Entity<Player>()
.Navigation(p => p.TrackedTimes)
.AutoInclude(false);
modelBuilder.Entity<Game>()
.Navigation(g => g.PlayedBy)
.AutoInclude(false);
modelBuilder.Entity<Game>()
.Navigation(g => g.TrackedTimes)
.AutoInclude(false);
modelBuilder.Entity<TrackedTime>()
.HasOne<Player>(p => p.Player)
.WithMany(p => p.TrackedTimes)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<TrackedTime>()
.Navigation(t => t.Player)
.AutoInclude();
modelBuilder.Entity<TrackedTime>()
.HasOne<Game>(p => p.Game)
.WithMany(g => g.TrackedTimes)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<TrackedTime>()
.Navigation(t => t.Game)
.AutoInclude();
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace SQLiteEF;
@ -7,6 +8,6 @@ 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!;
[JsonIgnore] public ICollection<Player> PlayedBy { get; init; } = null!;
[JsonIgnore] public ICollection<TrackedTime> TrackedTimes { get; init; } = null!;
}

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace SQLiteEF;
@ -10,13 +11,13 @@ public class Player : IUpdateable
public string ProfileUrl { get; set; }
public string AvatarUrl { get; set; }
public ICollection<Game> Games { get; init; } = null!;
public ICollection<TrackedTime> TrackedTimes { get; init; } = null!;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
[JsonIgnore] public ICollection<Game> Games { get; init; } = null!;
[JsonIgnore] public ICollection<TrackedTime> TrackedTimes { get; init; } = null!;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
public Player(ulong steamid, string name, string profileUrl, string avatarUrl)
public Player(ulong steamId, string name, string profileUrl, string avatarUrl)
{
this.SteamId = steamid;
this.SteamId = steamId;
this.Name = name;
this.ProfileUrl = profileUrl;
this.AvatarUrl = avatarUrl;
@ -27,9 +28,9 @@ public class Player : IUpdateable
/// <summary>
/// EF CORE
/// </summary>
internal Player(ulong steamid, string name, string profileUrl, string avatarUrl, DateTime updatedAt)
internal Player(ulong steamId, string name, string profileUrl, string avatarUrl, DateTime updatedAt)
{
this.SteamId = steamid;
this.SteamId = steamId;
this.Name = name;
this.ProfileUrl = profileUrl;
this.AvatarUrl = avatarUrl;

View File

@ -7,8 +7,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@ -1,12 +1,13 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace SQLiteEF;
[PrimaryKey("TimeStamp")]
public class TrackedTime
{
public Game Game { get; init; }
public Player Player { get; init; }
[JsonIgnore] public Game Game { get; init; }
[JsonIgnore] public Player Player { get; init; }
public DateTime TimeStamp { get; init; }
public ulong TimePlayed { get; init; }
@ -14,7 +15,7 @@ public class TrackedTime
{
this.Game = game;
this.Player = player;
this.TimeStamp = timeStamp??DateTime.Now;
this.TimeStamp = timeStamp??DateTime.UtcNow;
this.TimePlayed = timePlayed;
}