public void CreateSP(ProductSkladBM model) { using (var context = new DatabaseContext()) { ProductSklad element = model.Id.HasValue ? null : new ProductSklad(); if (model.Id.HasValue) { element = context.ProductSklads.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new ProductSklad(); context.ProductSklads.Add(element); } element.Count = model.Count; element.Data = model.Data; element.Status = model.Status; element.SkladId = model.SkladId; element.ProductId = model.ProductId; context.SaveChanges(); } }
public List <ProductSkladVM> ReadSP(ProductSkladBM model) { using (var context = new DatabaseContext()) { return(context.ProductSklads .Where(rec => model == null || rec.Id == model.Id || (rec.SkladId == model.SkladId && rec.ProductId == model.ProductId) || (rec.SkladId == model.SkladId && model.ProductId == 0)) .Select(rec => new ProductSkladVM { Id = rec.Id, Status = rec.Status, SkladId = rec.SkladId, ProductId = rec.ProductId, Data = rec.Data, Count = rec.Count }) .ToList()); } }