Skip to content

Commit

Permalink
Move SQLite other folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Del Rincón López committed Sep 28, 2023
1 parent f68da84 commit f0fccaa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions BlazorApp1/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

var app = builder.Build();

InitializeDatabase(app);
await InitializeDatabase(app);
InitializeAutoMapper(app);

// Configure the HTTP request pipeline.
Expand Down Expand Up @@ -43,11 +43,11 @@

app.Run();

static void InitializeDatabase(WebApplication app)
static async Task InitializeDatabase(WebApplication app)
{
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<WeatherDbContext>();
db.Database.Migrate();
await db.Database.MigrateAsync();
}

static void InitializeAutoMapper(WebApplication app)
Expand Down
10 changes: 9 additions & 1 deletion BlazorApp1/Shared/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BlazorApp1.Data.Abstractions.Repositories;
using BlazorApp1.Data.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace BlazorApp1.Data;
Expand All @@ -8,7 +9,14 @@ public static class DependencyInjectionExtensions
{
public static IServiceCollection AddWeatherForecastDataLayer(this IServiceCollection services)
{
services.AddDbContext<WeatherDbContext>();
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "data", "Local.sqlite");

if (!File.Exists(filePath))
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
}

services.AddDbContext<WeatherDbContext>(optionsBuilder => optionsBuilder.UseSqlite($"Data Source={filePath}"));
services.AddScoped<IWeatherForecastRepository, WeatherForecastRepository>();

return services;
Expand Down

0 comments on commit f0fccaa

Please sign in to comment.