コード例 #1
0
        public static void SeedSales(TuxedoDb db, IEnumerable <Sale> salesList)
        {
            foreach (var sale in salesList)
            {
                db.Sales.Add(new Sale()
                {
                    Id        = sale.Id,
                    StoreName = sale.StoreName,
                    TurnOver  = sale.TurnOver
                });
            }

            db.SaveChanges();
        }
コード例 #2
0
        public async Task TransferToMSSQL()
        {
            var contexSql = new TuxedoDb();

            var brands = (await this.GetData <Brand>("Brands")).ToList();
            var items  = (await this.GetData <Item>("Items")).ToList();

            try
            {
                using (contexSql)
                {
                    foreach (var brand in brands)
                    {
                        contexSql.Brands.Add(brand);
                    }

                    foreach (var item in items)
                    {
                        contexSql.Items.Add(item);
                    }
                    contexSql.SaveChanges();
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
コード例 #3
0
        public static void SeedItems(string excelFileName, TuxedoDb db)
        {
            var             connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\..\\..\\ExtractedExcelFiles\\tables\\{excelFileName}.xlsx; Extended Properties = \"Excel 12.0 Xml;HDR=YES\"";
            OleDbConnection connection       = new OleDbConnection(connectionString);

            connection.Open();

            OleDbCommand    command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
            OleDbDataReader reader  = command.ExecuteReader();

            while (reader.Read())
            {
                string Id         = reader.GetDouble(0).ToString();
                var    model      = (string)reader["Model"];
                var    BrandId    = reader.GetDouble(2).ToString();
                var    CountryId  = reader.GetDouble(3).ToString();
                var    ColorId    = reader.GetDouble(4).ToString();
                var    TypeId     = reader.GetDouble(5).ToString();
                var    MaterialId = reader.GetDouble(6).ToString();
                var    Price      = reader.GetDouble(7).ToString();

                Console.WriteLine($@"Item: {model} with ID {Id} added to SQL db");

                db.Items.Add(new Item()
                {
                    ID         = int.Parse(Id),
                    Model      = model,
                    BrandID    = int.Parse(BrandId),
                    CountryID  = int.Parse(CountryId),
                    ColorID    = int.Parse(ColorId),
                    TypeID     = int.Parse(TypeId),
                    MaterialID = int.Parse(MaterialId),
                    Price      = decimal.Parse(Price)
                });
            }

            db.SaveChanges();
        }