private void AddDataToSqlite(List<Product> products) { using (var db = new ToysSqliteContext()) { var sqliteProducts = db.Products; var sqliteProductsCount = db.Products.Count(); if (sqliteProductsCount != products.Count) { foreach (var item in products) { var product = new Product() { Sku = item.Sku, Description = item.Description, WholesalePrice = item.WholesalePrice, RetailPrice = item.RetailPrice, TradeDiscount = item.TradeDiscount, TradeDiscountRate = item.TradeDiscountRate, ManufacturerId = item.ManufacturerId }; sqliteProducts.Add(product); } } db.SaveChanges(); } }
private void SaveProductsToSqlServer(IEnumerable<BsonDocument> documents) { var product = new Product(); string sku; foreach (var document in documents) { sku = document["Sku"].ToString(); // Skip duplicate Sku values, because by design Sku is unique and Ef exploed with exception if (this.Data.Products.All().Any(x => x.Sku.Equals(sku))) { continue; } product.Sku = sku; product.Description = document["Description"].ToString(); product.WholesalePrice = decimal.Parse(document["WholesalePrice"].ToString(), CultureInfo.InvariantCulture); product.RetailPrice = decimal.Parse(document["RetailPrice"].ToString(), CultureInfo.InvariantCulture); product.TradeDiscount = decimal.Parse(document["TradeDiscount"].ToString(), CultureInfo.InvariantCulture); product.TradeDiscountRate = float.Parse(document["TradeDiscountRate"].ToString(), CultureInfo.InvariantCulture); product.ManufacturerId = int.Parse(document["ManufacturerId"].ToString(), CultureInfo.InvariantCulture); this.Data.Products.Add(product); this.Data.SaveChanges(); } }
public void SaveProductsToSqlServer(List<BsonDocument> documents) { var product = new Product(); foreach (var document in documents) { product.Sku = document["Sku"].ToString(); product.Description = document["Description"].ToString(); product.WholesalePrice = decimal.Parse(document["WholesalePrice"].ToString()); product.RetailPrice = decimal.Parse(document["RetailPrice"].ToString()); product.TradeDiscount = decimal.Parse(document["TradeDiscount"].ToString()); product.TradeDiscountRate = float.Parse(document["TradeDiscountRate"].ToString()); product.ManufacturerId = int.Parse(document["ManufacturerId"].ToString()); this.sqlServerDb.Products.Add(product); try { this.sqlServerDb.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex); } } //var product = new Product //{ //}; //this.sqlServerDb.Products.Add(product); //this.sqlServerDb.SaveChanges(); }