Create new DbContext constructor with external Options.
And install related packages.
This commit is contained in:
parent
ba77fbd176
commit
8fc6bc96c0
@ -10,4 +10,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="../EntityFrameworkCore.Data/EntityFrameworkCore.Data.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -1,9 +1,31 @@
|
|||||||
|
using EntityFrameworkCore.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||||
builder.Services.AddOpenApi();
|
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<DeadBallZoneLeagueDbContext>(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();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|||||||
@ -5,5 +5,8 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"SqliteDatabaseName": "DeadBallZoneLeague_EFCore.db"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,36 +1,26 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using EntityFrameworkCore.Domain;
|
using EntityFrameworkCore.Domain;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using Microsoft.EntityFrameworkCore.Design;
|
||||||
|
|
||||||
namespace EntityFrameworkCore.Data;
|
namespace EntityFrameworkCore.Data;
|
||||||
|
|
||||||
public class DeadBallZoneLeagueDbContext : DbContext
|
public class DeadBallZoneLeagueDbContext : DbContext
|
||||||
{
|
{
|
||||||
private string DbPath;
|
public DeadBallZoneLeagueDbContext(DbContextOptions<DeadBallZoneLeagueDbContext> options) : base(options)
|
||||||
public DeadBallZoneLeagueDbContext()
|
|
||||||
{
|
{
|
||||||
// In Ubuntu 24.04, the file is created here: /home/user/.local/share
|
// This constuctor allows it to accept the options set from other instances, such as in the API project.
|
||||||
var folder = Environment.SpecialFolder.LocalApplicationData;
|
|
||||||
var path = Environment.GetFolderPath(folder);
|
|
||||||
DbPath = Path.Combine(path, "DeadBallZoneLeague_EFCore.db");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbSet<Team> Teams { get; set; }
|
public DbSet<Team> Teams { get; set; }
|
||||||
public DbSet<Coach> Coaches { get; set; }
|
public DbSet<Coach> Coaches { get; set; }
|
||||||
public DbSet<League> Leagues { get; set; }
|
public DbSet<League> Leagues { get; set; }
|
||||||
public DbSet<Match> Matches { get; set; }
|
public DbSet<Match> Matches { get; set; }
|
||||||
public DbSet<TeamsAndLeaguesView> TeamsAndLeaguesView { get; set; }
|
public DbSet<TeamsAndLeaguesView> 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)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
// modelBuilder.ApplyConfiguration(new TeamConfiguration());
|
// modelBuilder.ApplyConfiguration(new TeamConfiguration());
|
||||||
@ -54,3 +44,24 @@ public class DeadBallZoneLeagueDbContext : DbContext
|
|||||||
|
|
||||||
public DateTime GetEarliestTeamMatch(int teamId) => throw new NotImplementedException();
|
public DateTime GetEarliestTeamMatch(int teamId) => throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class DeadBallZoneLeagueDbContextFactory : IDesignTimeDbContextFactory<DeadBallZoneLeagueDbContext>
|
||||||
|
{
|
||||||
|
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<DeadBallZoneLeagueDbContext>();
|
||||||
|
optionsBuilder.UseSqlite($"Data Source={dbPath}");
|
||||||
|
|
||||||
|
return new DeadBallZoneLeagueDbContext(optionsBuilder.Options);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,6 +9,9 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
12
README.md
12
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`
|
* `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
|
## Tips for efficient querying
|
||||||
|
|
||||||
* Use indexes.
|
* Use indexes.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user