🔧🗃️ Make Data/Program.cs executable, add Db stub, improve
continuous-integration/drone/push Build is passing Details

pull/104/head
Alexis Drai 3 years ago
parent 8514606fa6
commit 7c4c7bcfcb

@ -1,10 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory> <StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.9" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.9" />
@ -17,4 +20,5 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" /> <ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -10,47 +10,10 @@ namespace Data.EF
{ {
public class DiceAppDbContext : DbContext public class DiceAppDbContext : DbContext
{ {
public DbSet<PlayerEntity> Players { get; set; } public DbSet<PlayerEntity>? Players { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite("Data Source=EFDice.DiceApp.db"); => 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);
}
}
*/
} }
} }

@ -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<PlayerEntity>().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" }
);
}
}
}

@ -1,4 +1,5 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,9 +7,11 @@ using System.Threading.Tasks;
namespace Data.EF.Players namespace Data.EF.Players
{ {
[Index(nameof(Name), IsUnique = true)]
public class PlayerEntity public class PlayerEntity
{ {
public Guid ID { get; set; } public Guid ID { get; set; }
public string Name { get; set; } public string Name { get; set; }
} }
} }

@ -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}");
}
}
}
}
}
}
Loading…
Cancel
Save