From 709e22cacdf882134fb409919f4c0b713001ff5d Mon Sep 17 00:00:00 2001 From: Stedoss <29103029+Stedoss@users.noreply.github.com> Date: Sun, 30 Oct 2022 17:03:57 +0000 Subject: [PATCH] Update seeding to use database commits instead of migrations --- .../Data/Seed/LeedsBeerQuestSeeder.cs | 27 ++++++++++++++----- .../LeedsBeerQuest.API.csproj | 4 +++ .../LeedsBeerQuest.API/Program.cs | 9 +++++++ .../LeedsBeerQuest.API/ServiceDependencies.cs | 2 +- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs index 6e55229..be5c904 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs @@ -1,5 +1,6 @@ using System.Globalization; using CsvHelper; +using LeedsBeerQuest.API.Data.Contexts; using LeedsBeerQuest.API.Data.Models; using Microsoft.EntityFrameworkCore; @@ -7,17 +8,24 @@ namespace LeedsBeerQuest.API.Data.Seed; public class LeedsBeerQuestSeeder { - private readonly ModelBuilder _modelBuilder; + private readonly LeedsBeerQuestDbContext _dbContext; private readonly string _dataSourcePath; - public LeedsBeerQuestSeeder(ModelBuilder modelBuilder, string dataSourcePath) + public LeedsBeerQuestSeeder(LeedsBeerQuestDbContext dbContext, string dataSourcePath) { - _modelBuilder = modelBuilder; + _dbContext = dbContext; _dataSourcePath = dataSourcePath; } public void Seed() { + // We don't want to seed if we already have data in the database. + if (hasExistingData) + { + return; + } + + IEnumerable? lbqCSV; using (var reader = new StreamReader("../leedsbeerquest.csv")) @@ -29,8 +37,8 @@ public class LeedsBeerQuestSeeder var tags = getTags(lbqCSV).ToList(); var categories = getCategories(lbqCSV).ToList(); - _modelBuilder.Entity().HasData(tags); - _modelBuilder.Entity().HasData(categories); + _dbContext.Tags.AddRange(tags); + _dbContext.Categories.AddRange(categories); var venues = new List(); @@ -60,7 +68,9 @@ public class LeedsBeerQuestSeeder venueIndex++; } - _modelBuilder.Entity().HasData(venues); + _dbContext.Venues.AddRange(venues); + + _dbContext.SaveChangesAsync(); } private IEnumerable getTags(IEnumerable csvData) @@ -93,6 +103,11 @@ public class LeedsBeerQuestSeeder } } + private bool hasExistingData => + _dbContext.Venues.Any() || + _dbContext.Tags.Any() || + _dbContext.Categories.Any(); + private class LeedsBeerQuestCSV { public string name { get; set; } diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj index 2f0c4cc..eaa913b 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj @@ -6,6 +6,10 @@ enable + + TRACE + + diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs index 0d7497b..cf81416 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs @@ -1,4 +1,6 @@ using LeedsBeerQuest.API; +using LeedsBeerQuest.API.Data.Contexts; +using LeedsBeerQuest.API.Data.Seed; var builder = WebApplication.CreateBuilder(args); @@ -14,6 +16,13 @@ builder.Services.AddServiceDependencies(); var app = builder.Build(); + +#if LBQ_SEED_DATA +var dbContext = app.Services.GetRequiredService(); +var seeder = new LeedsBeerQuestSeeder(dbContext!, ""); +seeder.Seed(); +#endif + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs index c5cc4b5..b12936f 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs @@ -12,7 +12,7 @@ public static class ServiceDependencies var dbOptions = new DbContextOptionsBuilder(); dbOptions.UseSqlite("Data Source=lbq.db"); - serviceCollection.AddScoped(_ => new LeedsBeerQuestDbContext(dbOptions.Options)); + serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options)); serviceCollection.AddScoped(); } } \ No newline at end of file