示例#1
0
 /// <summary>
 /// Saves all the changes made on the aggregate
 /// </summary>
 public void SaveChanges()
 {
     try
     {
         _context.SaveChanges();
     }
     catch
     {
         throw;
     }
 }
        public void CleanAllData(RegistriesContext context)
        {
            if (context.ShippingAddresses.Any())
            {
                context.ShippingAddresses.RemoveRange(context.ShippingAddresses);
            }

            if (context.BillingInfos.Any())
            {
                context.BillingInfos.RemoveRange(context.BillingInfos);
            }

            if (context.Customers.Any())
            {
                context.Customers.RemoveRange(context.Customers);
            }

            context.SaveChanges();
        }
        /*
         * LoadDataFromFile(string fileName):
         * This method load the data from the given file into the Database
         * created by EntityFramework code first approach.
         */
        public void LoadDataFromFile(string fileName)
        {
            // Control question to avoid unwanted loading of data.
            Console.Clear();
            Console.Write("Are you sure that you want to load the Data to the SQL Server? ( Y / N ) : ");
            ConsoleKeyInfo k = Console.ReadKey(true);

            if (char.ToLower(k.KeyChar) == 'n')
            {
                return;                                 // If "NO", we exit the method without changes.
            }
            try
            {
                var lineas = File.ReadLines(fileName);

                foreach (string s in lineas)
                {
                    string[] datos = s.Split(',');

                    Register register = new Register();

                    register.Date        = DateTime.Parse(datos[0]);
                    register.IsExterior  = datos[1].ToLower() == "ute";
                    register.Temperature = double.Parse(datos[2].Replace('.', ',')); // Problem with Regional preferences in Operating system? Maybe Encoding. Check Later.
                    register.Humidity    = int.Parse(datos[3]);

                    database.Registros.Add(register);
                }
                database.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: " + e.Message);
                Environment.Exit(1);
            }
        }
 public void PrepareData(RegistriesContext context, Action <RegistriesContext> prepare)
 {
     prepare.Invoke(context);
     context.SaveChanges();
 }