From d06cca1ce33b6259ceb7c9c84855ea0d449ec633 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 12 Jun 2022 16:29:19 +0200 Subject: [PATCH 1/7] Added data model project --- I18N Commander/DataModel/DataModel.csproj | 10 ++++++++++ I18N Commander/I18N Commander.sln | 8 +++++++- I18N Commander/Processor/Processor.csproj | 4 ++++ I18N Commander/UI WinForms/UI WinForms.csproj | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 I18N Commander/DataModel/DataModel.csproj diff --git a/I18N Commander/DataModel/DataModel.csproj b/I18N Commander/DataModel/DataModel.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/I18N Commander/DataModel/DataModel.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/I18N Commander/I18N Commander.sln b/I18N Commander/I18N Commander.sln index f97190f..53e4473 100644 --- a/I18N Commander/I18N Commander.sln +++ b/I18N Commander/I18N Commander.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 17.3.32519.111 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Processor", "Processor\Processor.csproj", "{E24B7026-05BE-434D-9481-7CA5785BC7A8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UI WinForms", "UI WinForms\UI WinForms.csproj", "{5AE84E7C-3141-46CA-B390-4E42878B6195}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UI WinForms", "UI WinForms\UI WinForms.csproj", "{5AE84E7C-3141-46CA-B390-4E42878B6195}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataModel", "DataModel\DataModel.csproj", "{D18DD193-3F93-4D21-92DC-BA0E26B0342A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {5AE84E7C-3141-46CA-B390-4E42878B6195}.Debug|Any CPU.Build.0 = Debug|Any CPU {5AE84E7C-3141-46CA-B390-4E42878B6195}.Release|Any CPU.ActiveCfg = Release|Any CPU {5AE84E7C-3141-46CA-B390-4E42878B6195}.Release|Any CPU.Build.0 = Release|Any CPU + {D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/I18N Commander/Processor/Processor.csproj b/I18N Commander/Processor/Processor.csproj index a8fca95..94a5986 100644 --- a/I18N Commander/Processor/Processor.csproj +++ b/I18N Commander/Processor/Processor.csproj @@ -7,4 +7,8 @@ default + + + + diff --git a/I18N Commander/UI WinForms/UI WinForms.csproj b/I18N Commander/UI WinForms/UI WinForms.csproj index 9b0d15f..dc5c59a 100644 --- a/I18N Commander/UI WinForms/UI WinForms.csproj +++ b/I18N Commander/UI WinForms/UI WinForms.csproj @@ -11,6 +11,7 @@ + From 96c34e2947d5544e8311a713b22b9d70b8693ffb Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 12 Jun 2022 17:14:21 +0200 Subject: [PATCH 2/7] Added migration commands --- I18N Commander/DataModel/cmdAddMigration.cmd | 4 ++++ I18N Commander/DataModel/cmdUndoMigration.cmd | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 I18N Commander/DataModel/cmdAddMigration.cmd create mode 100644 I18N Commander/DataModel/cmdUndoMigration.cmd diff --git a/I18N Commander/DataModel/cmdAddMigration.cmd b/I18N Commander/DataModel/cmdAddMigration.cmd new file mode 100644 index 0000000..7d9244e --- /dev/null +++ b/I18N Commander/DataModel/cmdAddMigration.cmd @@ -0,0 +1,4 @@ +@echo off +set /p migrationName="Please enter the new migration's name: " +dotnet tool update --global dotnet-ef +dotnet ef migrations add %migrationName% diff --git a/I18N Commander/DataModel/cmdUndoMigration.cmd b/I18N Commander/DataModel/cmdUndoMigration.cmd new file mode 100644 index 0000000..992113f --- /dev/null +++ b/I18N Commander/DataModel/cmdUndoMigration.cmd @@ -0,0 +1,4 @@ +@echo off +set /p migrationName="Please enter the migration's name which should be the **ACTIVE** one: " +dotnet tool update --global dotnet-ef +dotnet ef database update %migrationName% From 89a4a8eab357aa37e0440acb27df2803069a133f Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 12 Jun 2022 17:14:48 +0200 Subject: [PATCH 3/7] Added main program for EF tooling --- I18N Commander/DataModel/Program.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 I18N Commander/DataModel/Program.cs diff --git a/I18N Commander/DataModel/Program.cs b/I18N Commander/DataModel/Program.cs new file mode 100644 index 0000000..d07d548 --- /dev/null +++ b/I18N Commander/DataModel/Program.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.Hosting; +namespace DataModel; + +public static class Program +{ + public static void Main(string[] args) + { + Console.WriteLine("This app is intended for the EF tooling. You cannot start this data project, though."); + Environment.Exit(0); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => Setup.Setup4EFTooling(args); +} \ No newline at end of file From 8c82f2608e7f9617de3d923b14da353b3a79c647 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 12 Jun 2022 17:15:09 +0200 Subject: [PATCH 4/7] Added setting entity --- I18N Commander/DataModel/Database/Setting.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 I18N Commander/DataModel/Database/Setting.cs diff --git a/I18N Commander/DataModel/Database/Setting.cs b/I18N Commander/DataModel/Database/Setting.cs new file mode 100644 index 0000000..3f847d6 --- /dev/null +++ b/I18N Commander/DataModel/Database/Setting.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; + +namespace DataModel.Database; + +public class Setting +{ + [Key] + public int Id { get; set; } + + public string Name { get; set; } = string.Empty; + + public string TextValue { get; set; } = string.Empty; + + public bool BoolValue { get; set; } + + public int IntegerValue { get; set; } + + public Guid GuidValue { get; set; } +} \ No newline at end of file From 19b9851832c6b4bc1bed8a74243f138c631b1e6e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 12 Jun 2022 17:15:30 +0200 Subject: [PATCH 5/7] Added data context --- I18N Commander/DataModel/DataModel.csproj | 14 ++++++++++ .../DataModel/Database/Common/DataContext.cs | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 I18N Commander/DataModel/Database/Common/DataContext.cs diff --git a/I18N Commander/DataModel/DataModel.csproj b/I18N Commander/DataModel/DataModel.csproj index 40c60dd..07d4dfb 100644 --- a/I18N Commander/DataModel/DataModel.csproj +++ b/I18N Commander/DataModel/DataModel.csproj @@ -7,4 +7,18 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/I18N Commander/DataModel/Database/Common/DataContext.cs b/I18N Commander/DataModel/Database/Common/DataContext.cs new file mode 100644 index 0000000..43d1c5d --- /dev/null +++ b/I18N Commander/DataModel/Database/Common/DataContext.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; + +namespace DataModel.Database.Common; + +public sealed class DataContext : DbContext +{ + public DbSet? Settings { get; set; } + + public DataContext(DbContextOptions contextOptions) : base(contextOptions) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + #region Ideas + + modelBuilder.Entity().HasIndex(n => n.Id); + modelBuilder.Entity().HasIndex(n => n.Name).IsUnique(); + modelBuilder.Entity().HasIndex(n => n.BoolValue); + modelBuilder.Entity().HasIndex(n => n.GuidValue); + modelBuilder.Entity().HasIndex(n => n.IntegerValue); + modelBuilder.Entity().HasIndex(n => n.TextValue); + + #endregion + } +} \ No newline at end of file From c0ffd582681b8489581663f483c4cc4d3017e44c Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 12 Jun 2022 17:16:01 +0200 Subject: [PATCH 6/7] Added setup code --- I18N Commander/DataModel/Setup.cs | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 I18N Commander/DataModel/Setup.cs diff --git a/I18N Commander/DataModel/Setup.cs b/I18N Commander/DataModel/Setup.cs new file mode 100644 index 0000000..44e32d8 --- /dev/null +++ b/I18N Commander/DataModel/Setup.cs @@ -0,0 +1,56 @@ +using DataModel.Database.Common; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace DataModel; + +public static class Setup +{ + private const string ENV_EF_TOOLING_DATABASE = "ENV_EF_TOOLING_DATABASE"; + private const string DB_READ_WRITE_MODE = "ReadWrite"; + private const string DB_READ_WRITE_CREATE_MODE = "ReadWriteCreate"; + + /// + /// Tries to migrate the data file. + /// + public static async Task PerformDataMigration(DataContext dbContext) + { + var pendingMigrations = (await dbContext.Database.GetPendingMigrationsAsync()).ToList(); + foreach (var pendingMigration in pendingMigrations) + { + Console.WriteLine($"The migration '{pendingMigration}' is pending."); + } + + await dbContext.Database.MigrateAsync(); + } + + /// + /// Add the database to the DI system + /// + public static void AddDatabase(this IServiceCollection serviceCollection, string path2DataFile, bool createWhenNecessary = true) + { + serviceCollection.AddDbContext(options => options.UseSqlite($"Filename={path2DataFile};Mode={(createWhenNecessary ? DB_READ_WRITE_CREATE_MODE : DB_READ_WRITE_MODE)}")); + } + + /// + /// Sets up the DI & db context ready for the EF tooling. + /// + public static IHostBuilder Setup4EFTooling(string[] args) + { + var dataFile = Environment.GetEnvironmentVariable(ENV_EF_TOOLING_DATABASE); + if (string.IsNullOrWhiteSpace(dataFile)) + { + Console.WriteLine("In order to use EF tooling, point the environment variable ENV_EF_TOOLING_DATABASE to the data file, which should be used for the EF tooling."); + Environment.Exit(100); + } + + var builder = Host.CreateDefaultBuilder(args); + builder.ConfigureServices((hostContext, serviceCollection) => + { + serviceCollection.AddDbContext(options => options.UseSqlite($"Filename={dataFile};Mode=ReadWriteCreate")); + }); + + return builder; + } +} From 1f23f88051f5128258b3beab6380076594ed3cfc Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 12 Jun 2022 17:16:11 +0200 Subject: [PATCH 7/7] Added initial migration --- ...220612151133_202206AddSettings.Designer.cs | 65 ++++++++++++++++++ .../20220612151133_202206AddSettings.cs | 67 +++++++++++++++++++ .../Migrations/DataContextModelSnapshot.cs | 63 +++++++++++++++++ project for migrations.i18nc | 0 4 files changed, 195 insertions(+) create mode 100644 I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.Designer.cs create mode 100644 I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.cs create mode 100644 I18N Commander/DataModel/Migrations/DataContextModelSnapshot.cs create mode 100644 project for migrations.i18nc diff --git a/I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.Designer.cs b/I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.Designer.cs new file mode 100644 index 0000000..50afbab --- /dev/null +++ b/I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.Designer.cs @@ -0,0 +1,65 @@ +// +using System; +using DataModel.Database.Common; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DataModel.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20220612151133_202206AddSettings")] + partial class _202206AddSettings + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "6.0.5"); + + modelBuilder.Entity("DataModel.Database.Setting", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("BoolValue") + .HasColumnType("INTEGER"); + + b.Property("GuidValue") + .HasColumnType("TEXT"); + + b.Property("IntegerValue") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("TextValue") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("BoolValue"); + + b.HasIndex("GuidValue"); + + b.HasIndex("Id"); + + b.HasIndex("IntegerValue"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("TextValue"); + + b.ToTable("Settings"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.cs b/I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.cs new file mode 100644 index 0000000..22de1af --- /dev/null +++ b/I18N Commander/DataModel/Migrations/20220612151133_202206AddSettings.cs @@ -0,0 +1,67 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DataModel.Migrations +{ + public partial class _202206AddSettings : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Settings", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: false), + TextValue = table.Column(type: "TEXT", nullable: false), + BoolValue = table.Column(type: "INTEGER", nullable: false), + IntegerValue = table.Column(type: "INTEGER", nullable: false), + GuidValue = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Settings", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Settings_BoolValue", + table: "Settings", + column: "BoolValue"); + + migrationBuilder.CreateIndex( + name: "IX_Settings_GuidValue", + table: "Settings", + column: "GuidValue"); + + migrationBuilder.CreateIndex( + name: "IX_Settings_Id", + table: "Settings", + column: "Id"); + + migrationBuilder.CreateIndex( + name: "IX_Settings_IntegerValue", + table: "Settings", + column: "IntegerValue"); + + migrationBuilder.CreateIndex( + name: "IX_Settings_Name", + table: "Settings", + column: "Name", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Settings_TextValue", + table: "Settings", + column: "TextValue"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Settings"); + } + } +} diff --git a/I18N Commander/DataModel/Migrations/DataContextModelSnapshot.cs b/I18N Commander/DataModel/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 0000000..4bfc41b --- /dev/null +++ b/I18N Commander/DataModel/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,63 @@ +// +using System; +using DataModel.Database.Common; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DataModel.Migrations +{ + [DbContext(typeof(DataContext))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "6.0.5"); + + modelBuilder.Entity("DataModel.Database.Setting", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("BoolValue") + .HasColumnType("INTEGER"); + + b.Property("GuidValue") + .HasColumnType("TEXT"); + + b.Property("IntegerValue") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("TextValue") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("BoolValue"); + + b.HasIndex("GuidValue"); + + b.HasIndex("Id"); + + b.HasIndex("IntegerValue"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("TextValue"); + + b.ToTable("Settings"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/project for migrations.i18nc b/project for migrations.i18nc new file mode 100644 index 0000000..e69de29