Skip to content

Commit

Permalink
upgrade to overridable db
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 6, 2024
1 parent c665de1 commit f55cc5f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 23 deletions.
16 changes: 12 additions & 4 deletions MyApp/Configure.Db.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using MyApp.Data;

[assembly: HostingStartup(typeof(MyApp.ConfigureDb))]

Expand All @@ -9,12 +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<IDbConnectionFactory>(new OrmLiteConnectionFactory(
context.Configuration.GetConnectionString("DefaultConnection")
?? ":memory:",
SqliteDialect.Provider));
connectionString, SqliteDialect.Provider));

// $ dotnet ef migrations add CreateIdentitySchema
// $ dotnet ef database update
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(connectionString, b => b.MigrationsAssembly(nameof(MyApp))));

// Enable built-in Database Admin UI at /admin-ui/database
services.AddPlugin(new AdminDatabaseFeature());
});
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MyApp/Migrations/ApplicationDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ partial class ApplicationDbContextModelSnapshot : ModelSnapshot
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.HasAnnotation("ProductVersion", "8.0.2");

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
Expand Down
6 changes: 0 additions & 6 deletions MyApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;

// $ dotnet ef migrations add CreateIdentitySchema
// $ dotnet ef database update
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(connectionString, b => b.MigrationsAssembly(nameof(MyApp))));
services.AddDatabaseDeveloperPageExceptionFilter();

services.AddAuthorization();
Expand Down
3 changes: 0 additions & 3 deletions MyApp/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"ConnectionStrings": {
"DefaultConnection": "DataSource=App_Data/app.db;Cache=Shared"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
7 changes: 0 additions & 7 deletions NuGet.Config

This file was deleted.

0 comments on commit f55cc5f

Please sign in to comment.