Created
August 3, 2016 03:07
-
-
Save julielerman/a2ff841c4d0ecd11a311df8b83cda052 to your computer and use it in GitHub Desktop.
Static class for seeding via EF, uses IServiceScopeFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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