Add EF Core + SQLite with empty AppDbContext

This commit is contained in:
2026-06-11 18:12:04 +00:00
parent 1eb0383834
commit 2c16a9485b
8 changed files with 120 additions and 17 deletions
+5
View File
@@ -9,3 +9,8 @@ backend/bin/
backend/obj/ backend/obj/
backend.tests/bin/ backend.tests/bin/
backend.tests/obj/ backend.tests/obj/
# SQLite dev database (created on first run)
backend/*.db
backend/*.db-shm
backend/*.db-wal
+7
View File
@@ -0,0 +1,7 @@
using Microsoft.EntityFrameworkCore;
namespace backend.Data;
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
}
+24
View File
@@ -0,0 +1,24 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using backend.Data;
#nullable disable
namespace backend.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20260611181007_Initial")]
partial class Initial
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.17");
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace backend.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
@@ -0,0 +1,21 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using backend.Data;
#nullable disable
namespace backend.Migrations
{
[DbContext(typeof(AppDbContext))]
partial class AppDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.17");
#pragma warning restore 612, 618
}
}
}
+14 -1
View File
@@ -1,7 +1,20 @@
using backend.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(opt =>
opt.UseSqlite(builder.Configuration.GetConnectionString("Default")));
var app = builder.Build(); var app = builder.Build();
app.MapGet("/", () => "Hello World!"); using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.Migrate();
}
app.MapGet("/health", () => Results.Ok(new { ok = true }));
app.Run(); app.Run();
+3
View File
@@ -1,4 +1,7 @@
{ {
"ConnectionStrings": {
"Default": "Data Source=./assistant.db"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
+8
View File
@@ -6,4 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.*">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.*" />
</ItemGroup>
</Project> </Project>