void AddProduct(string ProductName, string Category, int Quant, double Price, string newProducer, string[] Photos) { if (context.Products.Find(x => { return(x.Name == ProductName); }) == null) { if (context.Categories.Find(x => { return(x.Name == Category); }) == null) { context.Categories.Add(new Category { Name = Category }); context.SaveChanges(); } if (context.Producers.Find(x => { return(x.Name == Category); }) == null) { context.Producers.Add(new Producer { Name = newProducer }); context.SaveChanges(); } context.Products.Add(new Product { Name = ProductName, ProducerId = context.Producers.FirstOrDefault(x => { return(x.Name == "Decoy"); }).PoducerId, CategoryId = context.Categories.FirstOrDefault(x => { return(x.Name == "Крючки"); }).CategoryId, Quantity = new Random(0).Next(0, 1000), Price = new Random(0).NextDouble(), Photos = new List <Photo>() }); context.SaveChanges(); foreach (string item in Photos) { context.Photos.Add(new Photo { Path = item, ProductId = context.Products.FirstOrDefault(z => { return(z.Name == ProductName); }).PoductId }); } context.SaveChanges(); } }
public void SaveChanges() { context.SaveChanges(); }