-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepDb.cs
42 lines (38 loc) · 1.41 KB
/
PrepDb.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using FinSearchDataAcessLibrary.DataAccess;
using FinSearchDataAPI;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
namespace FInSearchAPI.Models
{
public static class PrepDb
{
public static void PreparePopulation(IApplicationBuilder app)
{
var resourcePath = @"Resources/Bloomberg-Exchange-Code-to-MIC-Mapping.csv";
using (var serviceScope = app.ApplicationServices.CreateScope())
{
SeedData(serviceScope.ServiceProvider.GetService<FinSearchDBContext>(), resourcePath);
}
}
public static void SeedData(FinSearchDBContext context, string resourcePath)
{
System.Console.WriteLine("Applying Migrations...");
context.Database.Migrate();
if (!context.BloomBergLookUp.Any())
{
System.Console.WriteLine("Adding data - seeding ");
var data = Utilities.GetExcelData(resourcePath);
if (data.Count == 0 )
throw new ArgumentNullException(nameof(data));
context.BloomBergLookUp.AddRange(data);
context.SaveChanges();
}
else {
System.Console.WriteLine("Already have data - not seeding.");
}
}
}
}