Rework
This commit is contained in:
33
SQLiteEF/Context.cs
Normal file
33
SQLiteEF/Context.cs
Normal 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
12
SQLiteEF/Game.cs
Normal 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
6
SQLiteEF/IUpdateable.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace SQLiteEF;
|
||||
|
||||
public interface IUpdateable
|
||||
{
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
29
SQLiteEF/TrackedTime.cs
Normal file
29
SQLiteEF/TrackedTime.cs
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user