private static void CreateSqlOrder() { using (var uow = CommerceModelUnitOfWork.UnitOfWork()) { //lets grab up a few products. var products = new List <Product>(); var asinList = new string[] { "B00000J1EQ", "B00002CF9M", "B00002S932", "B00004UE3D", "B00004XSAE" }; foreach (var asin in asinList) { var query = Query <Product> .Where(item => item.Asin == asin); var product = uow.Products.FirstOrDefault(item => item.Asin == asin); products.Add(product); } var customer = new Customer() { Address1 = "123 Main Street", City = "Simpsonville", Country = "US", Email = "*****@*****.**", FirstName = "John", LastName = "Doe", Phone = "555-867-5309", StateOrProvince = "SC" }; uow.Add(customer); var order = new Order() { Customer = customer, OrderDate = DateTime.Now, ShipTo = customer, Status = (int)OrderStatus.New }; uow.Add(order); foreach (var product in products) { var orderDetail = new OrderDetail() { Order = order, Product = product, OrderedQuantity = 2, Quantity = 2, UnitPrice = 19.95m }; uow.Add(orderDetail); } uow.SaveChanges(); } }
private static void PopulateSqlDatabase(List <MongoProduct> list) { using (var uow = CommerceModelUnitOfWork.UnitOfWork()) { var categories = uow.Categories.ToDictionary(item => item.CategoryName, StringComparer.OrdinalIgnoreCase); var publishers = uow.Publishers.ToDictionary(item => item.PublisherName, StringComparer.OrdinalIgnoreCase); foreach (var categoryName in list.Where(item => !string.IsNullOrWhiteSpace(item.Binding)).Select(item => item.Binding).ToList().Distinct()) { Category category = null; if (!categories.TryGetValue(categoryName, out category)) { category = new Category() { CategoryName = categoryName }; categories.Add(categoryName, category); uow.Add(category); } } foreach (var publisherName in list.Where(item => !string.IsNullOrWhiteSpace(item.Publisher)).Select(item => item.Publisher).ToList().Distinct()) { Publisher publisher = null; if (!publishers.TryGetValue(publisherName, out publisher)) { publisher = new Publisher() { PublisherName = publisherName }; publishers.Add(publisherName, publisher); uow.Add(publisher); } } foreach (var sourceProduct in list) { Category category = null; Publisher publisher = null; if (!string.IsNullOrWhiteSpace(sourceProduct.Binding)) { category = categories[sourceProduct.Binding]; } if (!string.IsNullOrWhiteSpace(sourceProduct.Publisher)) { publisher = publishers[sourceProduct.Publisher]; } if (sourceProduct.PackageDimensions == null) { sourceProduct.PackageDimensions = new Dimension(); } var product = new Product() { Asin = sourceProduct.ASIN, BuyItNowPrice = sourceProduct.BuyItNowPrice, Category = category, EsrbAgeRating = sourceProduct.ESRBAgeRating, Feature = string.Join(Environment.NewLine, sourceProduct.Feature), Format = sourceProduct.Format, Genre = sourceProduct.Genre.MaxLength(75), HardwarePlatform = sourceProduct.HardwarePlatform.MaxLength(75), Height = sourceProduct.PackageDimensions.Height, LargeImage = sourceProduct.LargeImage, Length = sourceProduct.PackageDimensions.Length, ListPrice = sourceProduct.ListPrice, Manufacturer = sourceProduct.Manufacturer, Model = sourceProduct.Model, PartNumber = sourceProduct.PartNumber, ProductGroup = sourceProduct.ProductGroup, Publisher = publisher, Title = sourceProduct.Title.MaxLength(250), Weight = sourceProduct.PackageDimensions.Weight, Width = sourceProduct.PackageDimensions.Width }; uow.Add(product); } var sw = new Stopwatch(); sw.Start(); uow.SaveChanges(); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); } }