From 1b35305c6d3ce3f98a4e0395da7189c4d010b540 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Wed, 6 Mar 2024 19:27:45 +0800 Subject: [PATCH] upgrade to overridable ef db mix --- MyApp/Configure.Db.cs | 21 ++++++++++++------- ...01000000_CreateIdentitySchema.Designer.cs} | 4 ++-- ...=> 20240301000000_CreateIdentitySchema.cs} | 0 .../ApplicationDbContextModelSnapshot.cs | 2 +- MyApp/Program.cs | 5 ----- 5 files changed, 17 insertions(+), 15 deletions(-) rename MyApp/Migrations/{20231019062137_CreateIdentitySchema.Designer.cs => 20240301000000_CreateIdentitySchema.Designer.cs} (98%) rename MyApp/Migrations/{20231019062137_CreateIdentitySchema.cs => 20240301000000_CreateIdentitySchema.cs} (100%) diff --git a/MyApp/Configure.Db.cs b/MyApp/Configure.Db.cs index 3403b7c..2f516e7 100644 --- a/MyApp/Configure.Db.cs +++ b/MyApp/Configure.Db.cs @@ -1,5 +1,7 @@ +using Microsoft.EntityFrameworkCore; using ServiceStack.Data; using ServiceStack.OrmLite; +using MyApp.Data; [assembly: HostingStartup(typeof(MyApp.ConfigureDb))] @@ -9,13 +11,18 @@ public class ConfigureDb : IHostingStartup { public void Configure(IWebHostBuilder builder) => builder .ConfigureServices((context, services) => { + var connectionString = context.Configuration.GetConnectionString("DefaultConnection") + ?? "DataSource=App_Data/app.db;Cache=Shared"; + services.AddSingleton(new OrmLiteConnectionFactory( - context.Configuration.GetConnectionString("DefaultConnection") - ?? ":memory:", - SqliteDialect.Provider)); - }) - .ConfigureAppHost(appHost => { + connectionString, SqliteDialect.Provider)); + + // $ dotnet ef migrations add CreateIdentitySchema + // $ dotnet ef database update + services.AddDbContext(options => + options.UseSqlite(connectionString, b => b.MigrationsAssembly(nameof(MyApp)))); + // Enable built-in Database Admin UI at /admin-ui/database - appHost.Plugins.Add(new AdminDatabaseFeature()); + services.AddPlugin(new AdminDatabaseFeature()); }); -} +} \ No newline at end of file diff --git a/MyApp/Migrations/20231019062137_CreateIdentitySchema.Designer.cs b/MyApp/Migrations/20240301000000_CreateIdentitySchema.Designer.cs similarity index 98% rename from MyApp/Migrations/20231019062137_CreateIdentitySchema.Designer.cs rename to MyApp/Migrations/20240301000000_CreateIdentitySchema.Designer.cs index a2832f2..cb96a69 100644 --- a/MyApp/Migrations/20231019062137_CreateIdentitySchema.Designer.cs +++ b/MyApp/Migrations/20240301000000_CreateIdentitySchema.Designer.cs @@ -11,14 +11,14 @@ namespace MyApp.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20231019062137_CreateIdentitySchema")] + [Migration("20240301000000_CreateIdentitySchema")] partial class CreateIdentitySchema { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "8.0.0-rc.2.23480.1"); + modelBuilder.HasAnnotation("ProductVersion", "8.0.2"); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => { diff --git a/MyApp/Migrations/20231019062137_CreateIdentitySchema.cs b/MyApp/Migrations/20240301000000_CreateIdentitySchema.cs similarity index 100% rename from MyApp/Migrations/20231019062137_CreateIdentitySchema.cs rename to MyApp/Migrations/20240301000000_CreateIdentitySchema.cs diff --git a/MyApp/Migrations/ApplicationDbContextModelSnapshot.cs b/MyApp/Migrations/ApplicationDbContextModelSnapshot.cs index d544a5b..8984a6a 100644 --- a/MyApp/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/MyApp/Migrations/ApplicationDbContextModelSnapshot.cs @@ -15,7 +15,7 @@ partial class ApplicationDbContextModelSnapshot : ModelSnapshot protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "8.0.0-rc.2.23480.1"); + modelBuilder.HasAnnotation("ProductVersion", "8.0.2"); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => { diff --git a/MyApp/Program.cs b/MyApp/Program.cs index 43cfd2a..f77fdf1 100644 --- a/MyApp/Program.cs +++ b/MyApp/Program.cs @@ -33,11 +33,6 @@ services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo("App_Data")); -// $ dotnet ef migrations add CreateIdentitySchema -// $ dotnet ef database update -var connectionString = config.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); -services.AddDbContext(options => - options.UseSqlite(connectionString, b => b.MigrationsAssembly(nameof(MyApp)))); services.AddDatabaseDeveloperPageExceptionFilter(); services.AddIdentityCore(options => options.SignIn.RequireConfirmedAccount = true)