예제 #1
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <WeatherDbContext>();
                    WeatherDbContext.Initialize(context);
                }
                catch (Exception e)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(e, "Something went wrong when initializing the database");
                }
            }
            host.Run();
        }
예제 #2
0
        public static void Initialize(WeatherDbContext context)
        {
            context.Database.EnsureCreated();
            var         readWeather    = context.Enviornments.FirstOrDefault();
            CultureInfo commaCorrector = CultureInfo.InvariantCulture;

            if (readWeather is null)
            {
                string        WeatherPath  = "TempFuktData.csv";
                List <string> weatherLines = File.ReadAllLines(WeatherPath)
                                             .Skip(1)
                                             .Distinct()
                                             .ToList();

                int errorCount = 0;

                if (weatherLines.Count > 0)
                {
                    foreach (var line in weatherLines)
                    {
                        var weatherData = line.Split(',');

                        Enviornment env = new Enviornment();
                        try
                        {
                            env.Date        = Convert.ToDateTime(weatherData[0]);
                            env.Humidity    = Convert.ToInt32(weatherData[3]);
                            env.Temperature = (float)Convert.ToDouble(weatherData[2], commaCorrector);
                            if (weatherData[1] == "Inne")
                            {
                                env.InsideOrOutside = 1;
                            }
                            else
                            {
                                env.InsideOrOutside = 2;
                            }
                            context.Enviornments.Add(env);
                        }
                        catch (FormatException e)
                        {
                            //Hamnar här ibland för -tecknet är nånting annat
                            float  f;
                            int    i;
                            string s = "-" + weatherData[2].Substring(1);

                            bool b = int.TryParse(s, NumberStyles.AllowLeadingSign, commaCorrector, out i);
                            f = (float)i;
                            env.Temperature = f;
                            //env.Temperature = (float)int.Parse(weatherData[2]);
                        }
                        catch (Exception e)
                        {
                            errorCount++;
                            //Console.WriteLine(e.Message);
                        }
                    }
                    Console.WriteLine($"{errorCount} rows failed to load.");
                    context.SaveChanges();
                }
            }
        }