working through how to seed database

This commit is contained in:
StevanFreeborn
2022-06-20 08:50:11 -05:00
parent a76320df92
commit 545dcb4215
3 changed files with 44 additions and 7 deletions
+1 -7
View File
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.OpenApi.Writers;
using MongoDB.Driver;
using MongoDB.Driver;
using server.Models;
namespace server.Persistence
+34
View File
@@ -0,0 +1,34 @@
using System.Runtime.CompilerServices;
using MongoDB.Driver;
using server.Models;
namespace server.Persistence.Seed
{
public class Seeder
{
private readonly IMongoClient client;
private readonly IMongoDatabase database;
private readonly IConfiguration configuration;
private readonly IMongoCollection<Season> Seasons;
public Seeder()
{
configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
client = new MongoClient(configuration.GetSection("MongoDBSettings:ConnectionString").Value);
database = client.GetDatabase(configuration.GetSection("MongoDBSettings:DatabaseName").Value);
Seasons = database.GetCollection<Season>(configuration.GetSection("MongoDBSettings:SeasonsCollection").Value);
}
public async Task SeedAsync()
{
var seasons = await Seasons.Find(season => true).ToListAsync();
foreach (var season in seasons)
{
Console.WriteLine(season);
}
}
}
}
+9
View File
@@ -2,6 +2,13 @@ using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using server.Persistence;
using server.Persistence.Repositories;
using server.Persistence.Seed;
if (args.Length == 1 && args[0].ToLower() == "seed")
{
var seeder = new Seeder();
_ = seeder.SeedAsync();
}
var builder = WebApplication.CreateBuilder(args);
@@ -42,3 +49,5 @@ app.UseAuthorization();
app.MapControllers();
app.Run();