Skip to content

Instantly share code, notes, and snippets.

@julielerman
Created August 3, 2016 03:07
Show Gist options
  • Save julielerman/a2ff841c4d0ecd11a311df8b83cda052 to your computer and use it in GitHub Desktop.
Save julielerman/a2ff841c4d0ecd11a311df8b83cda052 to your computer and use it in GitHub Desktop.
Static class for seeding via EF, uses IServiceScopeFactory
public static class Seeder
{
public static void Seedit(string jsonData, IServiceProvider serviceProvider)
{
List<WeatherEvent> events =
JsonConvert.DeserializeObject<List<WeatherEvent>>(jsonData);
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<WeatherContext>();
if (!context.WeatherEvents.Any())
{
context.AddRange(events);
context.SaveChanges();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment