コード例 #1
0
        private Product GetOrCreateProduct(string productName , double price)
        {
            var product = this.db.Products.FirstOrDefault(p => p.ProductName == productName);
            if (product == null)
            {
                product = new Product()
                {
                    ProductName = productName,
                    Price = (decimal)price,
                    MeasureId = 2,
                    Vendors = new Vendor { VendorName = productName.Split(' ').Last() + " Corp." }
                };

                this.db.Products.Add(product);
                this.db.SaveChanges();
            }

            return product;
        }
コード例 #2
0
        private static Product GetOrCreateProduct(string productName, ChainOfSupermarketsContext db)
        {
            var product = db.Products.FirstOrDefault(p => p.ProductName == productName);
            if (product == null)
            {
                product = new Product()
                {
                    ProductName = productName,
                    Vendors = new Vendor { VendorName = productName.Split(' ').Last() + " Corp." }
                };

                db.Products.Add(product);
                db.SaveChanges();
            }

            return product;
        }