From 7c4c7bcfcb2541cc874c7899fa235805fd158b52 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Sat, 1 Oct 2022 22:37:01 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=F0=9F=97=83=EF=B8=8F=20Make=20Data?= =?UTF-8?q?/Program.cs=20executable,=20add=20Db=20stub,=20improve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Data/Data.csproj | 18 ++++++---- Sources/Data/EF/DiceAppDbContext.cs | 39 +-------------------- Sources/Data/EF/DiceAppDbContextWithStub.cs | 25 +++++++++++++ Sources/Data/EF/Players/PlayerEntity.cs | 5 ++- Sources/Data/Program.cs | 30 ++++++++++++++++ 5 files changed, 71 insertions(+), 46 deletions(-) create mode 100644 Sources/Data/EF/DiceAppDbContextWithStub.cs create mode 100644 Sources/Data/Program.cs diff --git a/Sources/Data/Data.csproj b/Sources/Data/Data.csproj index 749750b..7e30ad7 100644 --- a/Sources/Data/Data.csproj +++ b/Sources/Data/Data.csproj @@ -1,20 +1,24 @@ - + + + Exe net6.0 enable enable $(MSBuildProjectDirectory) + - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/Sources/Data/EF/DiceAppDbContext.cs b/Sources/Data/EF/DiceAppDbContext.cs index 9422a45..2140253 100644 --- a/Sources/Data/EF/DiceAppDbContext.cs +++ b/Sources/Data/EF/DiceAppDbContext.cs @@ -10,47 +10,10 @@ namespace Data.EF { public class DiceAppDbContext : DbContext { - public DbSet Players { get; set; } + public DbSet? Players { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlite("Data Source=EFDice.DiceApp.db"); - /* test with this - - > dotnet ef migrations add person_test - - > dotnet ef database update - - ... - - using (DiceAppDbContext db = new()) - { - db.Players.AddRange(PlayerExtensions.ToEntities(new Player[] { - new("Alice"), - new("Bob"), - new("Clyde"), - new("Fucking Kevin GOSH") - })); - - Console.WriteLine("Added, not saved"); - if (db.Players is not null) - { - foreach (PlayerEntity p in db.Players) - { - Console.WriteLine(p.ID + " - " + p.Name); - } - } - - db.SaveChanges(); - - Console.WriteLine("Saved"); - foreach (PlayerEntity p in db.Players) - { - Console.WriteLine(p.ID + " - " + p.Name); - } - } - */ - - } } diff --git a/Sources/Data/EF/DiceAppDbContextWithStub.cs b/Sources/Data/EF/DiceAppDbContextWithStub.cs new file mode 100644 index 0000000..89e2672 --- /dev/null +++ b/Sources/Data/EF/DiceAppDbContextWithStub.cs @@ -0,0 +1,25 @@ +using Data.EF.Players; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data.EF +{ + internal class DiceAppDbContextWithStub : DiceAppDbContext + { + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity().HasData( + new PlayerEntity { ID = Guid.Parse("e3b42372-0186-484c-9b1c-01618fbfac44"), Name = "Alice" }, + new PlayerEntity { ID = Guid.Parse("73265e15-3c43-45f8-8f5d-d02feaaf7620"), Name = "Bob" }, + new PlayerEntity { ID = Guid.Parse("5198ba9d-44d6-4660-85f9-1843828c6f0d"), Name = "Clyde" }, + new PlayerEntity { ID = Guid.Parse("386cec27-fd9d-4475-8093-93c8b569bf2e"), Name = "Dahlia" } + ); + } + } +} diff --git a/Sources/Data/EF/Players/PlayerEntity.cs b/Sources/Data/EF/Players/PlayerEntity.cs index 7d5a34f..aa2d14c 100644 --- a/Sources/Data/EF/Players/PlayerEntity.cs +++ b/Sources/Data/EF/Players/PlayerEntity.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,9 +7,11 @@ using System.Threading.Tasks; namespace Data.EF.Players { + [Index(nameof(Name), IsUnique = true)] public class PlayerEntity { public Guid ID { get; set; } + public string Name { get; set; } } } diff --git a/Sources/Data/Program.cs b/Sources/Data/Program.cs new file mode 100644 index 0000000..b448831 --- /dev/null +++ b/Sources/Data/Program.cs @@ -0,0 +1,30 @@ + +using Data.EF; +using Data.EF.Players; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Model.Players; +using System.Collections; +using System.ComponentModel; +using System.Diagnostics; +using System.Runtime.Intrinsics.Arm; + +namespace Data +{ + class Program + { + static void Main(string[] args) + { + using (DiceAppDbContext db = new DiceAppDbContextWithStub()) // we will remove the "WithStub" bit when we release + { + if (db.Players is not null) + { + foreach (PlayerEntity entity in db.Players) + { + Debug.WriteLine($"{entity.ID} -- {entity.Name}"); + } + } + } + } + } +} \ No newline at end of file