🔧🗃️ 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,20 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -10,47 +10,10 @@ namespace Data.EF
{
public class DiceAppDbContext : DbContext
{
public DbSet<PlayerEntity> Players { get; set; }
public DbSet<PlayerEntity>? 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);
}
}
*/
}
}

@ -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.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; }
}
}

@ -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