Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/DemoApplication.Api/DemoApplication.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<ProjectReference Include="..\DemoApplication.Application\DemoApplication.Application.csproj" />
<ProjectReference Include="..\DemoApplication.Infrastructure\DemoApplication.Infrastructure.csproj" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/DemoApplication.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
builder.Services.AddProblemDetails();
builder.Services.AddHealthChecks();
builder.Services.AddControllers();
builder.Services.AddPersistence(builder.Configuration, builder.Environment.ContentRootPath);
builder.Services.AddSingleton<IStartupCheck, StartupCheck>();
builder.Services.AddHostedService<StartupValidationHostedService>();

Expand Down
5 changes: 4 additions & 1 deletion src/DemoApplication.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Persistence": {
"DatabasePath": "App_Data/demo-application-development.db"
}
}
5 changes: 4 additions & 1 deletion src/DemoApplication.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Persistence": {
"DatabasePath": "App_Data/demo-application.db"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.1.12" />
<ProjectReference Include="..\DemoApplication.Application\DemoApplication.Application.csproj" />
<ProjectReference Include="..\DemoApplication.Domain\DemoApplication.Domain.csproj" />
</ItemGroup>
Expand Down
57 changes: 57 additions & 0 deletions src/DemoApplication.Infrastructure/DemoApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.EntityFrameworkCore;

namespace DemoApplication.Infrastructure;

/// <summary>Owns the SQLite persistence model and its deterministic baseline seed data.</summary>
public sealed class DemoApplicationDbContext : DbContext
{
/// <summary>Initializes the context with configured provider options.</summary>
/// <param name="options">Provider and database options.</param>
public DemoApplicationDbContext(DbContextOptions<DemoApplicationDbContext> options)
: base(options)
{
}

/// <summary>Gets the baseline roles.</summary>
public DbSet<PersistenceRole> Roles => Set<PersistenceRole>();

/// <summary>Gets the minimal order index records.</summary>
public DbSet<OrderIndexRecord> Orders => Set<OrderIndexRecord>();

/// <summary>Gets the minimal shipment index records.</summary>
public DbSet<ShipmentIndexRecord> Shipments => Set<ShipmentIndexRecord>();

/// <summary>Configures table names, indexes, keys, and deterministic seed records.</summary>
/// <param name="modelBuilder">Entity model builder supplied by Entity Framework Core.</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Keep the schema foundation independent from future business workflow entities.
modelBuilder.Entity<PersistenceRole>(entity =>
{
entity.ToTable("Roles");
entity.HasKey(role => role.Id);
entity.Property(role => role.Name).IsRequired().HasMaxLength(64);
entity.Property(role => role.NormalizedName).IsRequired().HasMaxLength(64);
entity.HasIndex(role => role.NormalizedName).IsUnique();
entity.HasData(
new PersistenceRole { Id = new Guid("2e6bd8e6-1a43-4ec2-a2ef-6e9e2ef7f44c"), Name = "Administrator", NormalizedName = "ADMINISTRATOR" },
new PersistenceRole { Id = new Guid("d9aa8d4e-969c-4ea8-9b11-1c3f4d3b35f2"), Name = "User", NormalizedName = "USER" });
});

modelBuilder.Entity<OrderIndexRecord>(entity =>
{
entity.ToTable("Orders");
entity.HasKey(order => order.Id);
entity.Property(order => order.Status).IsRequired().HasMaxLength(32);
entity.HasIndex(order => new { order.Status, order.CreatedUtc });
});

modelBuilder.Entity<ShipmentIndexRecord>(entity =>
{
entity.ToTable("Shipments");
entity.HasKey(shipment => shipment.Id);
entity.Property(shipment => shipment.Status).IsRequired().HasMaxLength(32);
entity.HasIndex(shipment => new { shipment.OrderId, shipment.Status });
});
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace DemoApplication.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class InitialPersistence : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
CreatedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Roles",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
NormalizedName = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Roles", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Shipments",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
OrderId = table.Column<Guid>(type: "TEXT", nullable: false),
Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
UpdatedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shipments", x => x.Id);
});

migrationBuilder.InsertData(
table: "Roles",
columns: new[] { "Id", "Name", "NormalizedName" },
values: new object[,]
{
{ new Guid("2e6bd8e6-1a43-4ec2-a2ef-6e9e2ef7f44c"), "Administrator", "ADMINISTRATOR" },
{ new Guid("d9aa8d4e-969c-4ea8-9b11-1c3f4d3b35f2"), "User", "USER" }
});

migrationBuilder.CreateIndex(
name: "IX_Orders_Status_CreatedUtc",
table: "Orders",
columns: new[] { "Status", "CreatedUtc" });

migrationBuilder.CreateIndex(
name: "IX_Roles_NormalizedName",
table: "Roles",
column: "NormalizedName",
unique: true);

migrationBuilder.CreateIndex(
name: "IX_Shipments_OrderId_Status",
table: "Shipments",
columns: new[] { "OrderId", "Status" });
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");

migrationBuilder.DropTable(
name: "Roles");

migrationBuilder.DropTable(
name: "Shipments");
}
}
}
Loading