Update seeding to use database commits instead of migrations

This commit is contained in:
Stedoss 2022-10-30 17:03:57 +00:00
parent bbcc1b5e22
commit 709e22cacd
4 changed files with 35 additions and 7 deletions

View File

@ -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<LeedsBeerQuestCSV>? 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<Tag>().HasData(tags);
_modelBuilder.Entity<Category>().HasData(categories);
_dbContext.Tags.AddRange(tags);
_dbContext.Categories.AddRange(categories);
var venues = new List<Venue>();
@ -60,7 +68,9 @@ public class LeedsBeerQuestSeeder
venueIndex++;
}
_modelBuilder.Entity<Venue>().HasData(venues);
_dbContext.Venues.AddRange(venues);
_dbContext.SaveChangesAsync();
}
private IEnumerable<Tag> getTags(IEnumerable<LeedsBeerQuestCSV> 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; }

View File

@ -6,6 +6,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="29.0.0" />
<PackageReference Include="EntityFramework" Version="6.4.4" />

View File

@ -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<LeedsBeerQuestDbContext>();
var seeder = new LeedsBeerQuestSeeder(dbContext!, "");
seeder.Seed();
#endif
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{

View File

@ -12,7 +12,7 @@ public static class ServiceDependencies
var dbOptions = new DbContextOptionsBuilder<LeedsBeerQuestDbContext>();
dbOptions.UseSqlite("Data Source=lbq.db");
serviceCollection.AddScoped(_ => new LeedsBeerQuestDbContext(dbOptions.Options));
serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options));
serviceCollection.AddScoped<IVenueService, VenueService>();
}
}