Beispiel #1
0
        public static async Task SeedAsync(FarmContext farmContext, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!farmContext.Farms.Any())
                {
                    var farmData = File.ReadAllText("../Infrastructure/SeedData/farms.json");
                    var farms    = JsonConvert.DeserializeObject <List <Farm> >(farmData);
                    farmContext.Farms.AddRange(farms);
                    await farmContext.SaveChangesAsync();
                }

                if (!farmContext.Cows.Any())
                {
                    var cowsData = File.ReadAllText("../Infrastructure/SeedData/cows.json");
                    var cows     = JsonConvert.DeserializeObject <List <Cow> >(cowsData);
                    farmContext.Cows.AddRange(cows);
                    await farmContext.SaveChangesAsync();
                }

                if (!farmContext.Sensors.Any())
                {
                    var sensorsData = File.ReadAllText("../Infrastructure/SeedData/sensors.json");
                    var sensors     = JsonConvert.DeserializeObject <List <Sensor> >(sensorsData);
                    farmContext.Sensors.AddRange(sensors);
                    await farmContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <FarmContextSeed>();
                logger.LogError(ex, "Failed while seeding");
            }
        }
Beispiel #2
0
 public async Task <int> Complete()
 {
     return(await _farmContext.SaveChangesAsync());
 }