From 8fc6bc96c01d93e031809b23f48502cd9c51d03c Mon Sep 17 00:00:00 2001 From: Kevin Matsubara Date: Mon, 14 Apr 2025 10:50:13 +0200 Subject: [PATCH] Create new DbContext constructor with external Options. And install related packages. --- .../EntityFrameworkCore.API.csproj | 3 ++ EntityFrameworkCore.API/Program.cs | 22 ++++++++++ EntityFrameworkCore.API/appsettings.json | 5 ++- .../DeadBallZoneLeagueDbContext.cs | 41 ++++++++++++------- .../EntityFrameworkCore.Data.csproj | 3 ++ README.md | 12 ++++++ 6 files changed, 70 insertions(+), 16 deletions(-) diff --git a/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj b/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj index 4117b6a..440c906 100644 --- a/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj +++ b/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj @@ -10,4 +10,7 @@ + + + diff --git a/EntityFrameworkCore.API/Program.cs b/EntityFrameworkCore.API/Program.cs index ee9d65d..0ac6d01 100644 --- a/EntityFrameworkCore.API/Program.cs +++ b/EntityFrameworkCore.API/Program.cs @@ -1,9 +1,31 @@ +using EntityFrameworkCore.Data; +using Microsoft.EntityFrameworkCore; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); +var sqliteDatabaseName = builder.Configuration.GetConnectionString("SqliteDatabaseName"); +var folder = Environment.SpecialFolder.LocalApplicationData; +var path = Environment.GetFolderPath(folder); +var dbPath = Path.Combine(path, sqliteDatabaseName); +var connectionString = $"Data Source={dbPath}"; + +builder.Services.AddDbContext(options => { + options.UseSqlite(connectionString) + .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) + .LogTo(Console.WriteLine, LogLevel.Information); + + if (!builder.Environment.IsProduction()) + { + // Do not allow this in Production. + options.EnableSensitiveDataLogging(); + options.EnableDetailedErrors(); + } +}); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/EntityFrameworkCore.API/appsettings.json b/EntityFrameworkCore.API/appsettings.json index 10f68b8..4c58f0a 100644 --- a/EntityFrameworkCore.API/appsettings.json +++ b/EntityFrameworkCore.API/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "SqliteDatabaseName": "DeadBallZoneLeague_EFCore.db" + } } diff --git a/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs b/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs index f8dc8dc..d5b6b93 100644 --- a/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs +++ b/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs @@ -1,36 +1,26 @@ using Microsoft.EntityFrameworkCore; using EntityFrameworkCore.Domain; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System.Reflection; +using Microsoft.EntityFrameworkCore.Design; namespace EntityFrameworkCore.Data; public class DeadBallZoneLeagueDbContext : DbContext { - private string DbPath; - public DeadBallZoneLeagueDbContext() + public DeadBallZoneLeagueDbContext(DbContextOptions options) : base(options) { - // In Ubuntu 24.04, the file is created here: /home/user/.local/share - var folder = Environment.SpecialFolder.LocalApplicationData; - var path = Environment.GetFolderPath(folder); - DbPath = Path.Combine(path, "DeadBallZoneLeague_EFCore.db"); + // This constuctor allows it to accept the options set from other instances, such as in the API project. } + public DbSet Teams { get; set; } public DbSet Coaches { get; set; } public DbSet Leagues { get; set; } public DbSet Matches { get; set; } public DbSet TeamsAndLeaguesView { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - optionsBuilder.UseSqlite($"Data source={DbPath}") - // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // Do not use tracking, set globally on all queries. - .LogTo(Console.WriteLine, LogLevel.Information) - .EnableSensitiveDataLogging() // Do not allow this in Production. - .EnableDetailedErrors(); // Do not allow this in Production. - } - protected override void OnModelCreating(ModelBuilder modelBuilder) { // modelBuilder.ApplyConfiguration(new TeamConfiguration()); @@ -54,3 +44,24 @@ public class DeadBallZoneLeagueDbContext : DbContext public DateTime GetEarliestTeamMatch(int teamId) => throw new NotImplementedException(); } + +public class DeadBallZoneLeagueDbContextFactory : IDesignTimeDbContextFactory +{ + public DeadBallZoneLeagueDbContext CreateDbContext(string[] args) + { + var folder = Environment.SpecialFolder.LocalApplicationData; + var path = Environment.GetFolderPath(folder); + + IConfigurationRoot configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .Build(); + + var dbPath = Path.Combine(path, configuration.GetConnectionString("SqliteDatabaseName")); + + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlite($"Data Source={dbPath}"); + + return new DeadBallZoneLeagueDbContext(optionsBuilder.Options); + } +} \ No newline at end of file diff --git a/EntityFrameworkCore.Data/EntityFrameworkCore.Data.csproj b/EntityFrameworkCore.Data/EntityFrameworkCore.Data.csproj index 01ad723..37aafa1 100644 --- a/EntityFrameworkCore.Data/EntityFrameworkCore.Data.csproj +++ b/EntityFrameworkCore.Data/EntityFrameworkCore.Data.csproj @@ -9,6 +9,9 @@ + + + diff --git a/README.md b/README.md index adbe381..19cc246 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,18 @@ Note that the [Microsoft.EntityFrameworkCore.Tools](https://www.nuget.org/packag * `dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 9.0.3` +[Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration/9.0.3) + +* `dotnet add package Microsoft.Extensions.Configuration --version 9.0.3` + +[Microsoft.Extensions.Configuration.FileExtensions](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.FileExtensions/9.0.3) + +* `dotnet add package Microsoft.Extensions.Configuration.FileExtensions --version 9.0.3` + +[Microsoft.Extensions.Configuration.Json](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/9.0.3) + +* `dotnet add package Microsoft.Extensions.Configuration.Json --version 9.0.3` + ## Tips for efficient querying * Use indexes.