private static void UpdateCompany(DSModel db, KeyBinder key, CompanyModel model) { Company poco = db.Companies.Where(c => c.CompanyID == model.CompanyID).FirstOrDefault(); if (poco == null) { throw new ArgumentException("No company with the specified ID!"); } poco.CompanyID = model.CompanyID; poco.CompanyName = model.CompanyName; poco.CompanyCode = model.CompanyCode; poco.CompanyAddress1 = model.CompanyAddress1; poco.CompanyAddress2 = model.CompanyAddress2; poco.CompanyCity = model.CompanyCity; poco.CompanyState = model.CompanyState; poco.CompanyPostCode = model.CompanyPostCode; poco.CompanyContactName = model.CompanyContactName; poco.CompanyFax = model.CompanyFax; poco.CompanyPhone = model.CompanyPhone; poco.CompanyEmail = model.CompanyEmail; poco.LunchTime = model.LunchTime; poco.TrainingTime = model.TrainingTime; poco.IsEnabled = model.IsEnabled; List <Location> locationsToBeDeleted = poco.Locations.Where(dl => !model.Locations.Any(ml => ml.LocationID == dl.LocationID)).ToList(); foreach (var del in locationsToBeDeleted) { db.Delete(del); poco.Locations.Remove(del); } foreach (var ins in model.Locations) { Location loc = LocationRepository.SaveLocation(db, key, ins, poco); poco.Locations.Add(loc); key.AddKey(loc, ins, ins.GetName(p => p.LocationID)); } }
private static void InsertCompany(DSModel db, KeyBinder key, CompanyModel model) { Company poco = new Company(); poco.CompanyID = model.CompanyID; poco.CompanyName = model.CompanyName; if (model.CompanyCode == string.Empty) { poco.CompanyCode = "C" + CompanyRepository.PeekCompanyCode(db, "C"); } else { poco.CompanyCode = model.CompanyCode; } poco.CompanyAddress1 = model.CompanyAddress1; poco.CompanyAddress2 = model.CompanyAddress2; poco.CompanyCity = model.CompanyCity; poco.CompanyState = model.CompanyState; poco.CompanyPostCode = model.CompanyPostCode; poco.CompanyContactName = model.CompanyContactName; poco.CompanyFax = model.CompanyFax; poco.CompanyPhone = model.CompanyPhone; poco.CompanyEmail = model.CompanyEmail; poco.LunchTime = model.LunchTime; poco.TrainingTime = model.TrainingTime; poco.IsEnabled = model.IsEnabled; foreach (var loc in model.Locations) { Location l = LocationRepository.SaveLocation(db, key, loc, poco); poco.Locations.Add(l); key.AddKey(l, loc, loc.GetName(p => p.LocationID)); key.AddKey(poco, loc, loc.GetName(p => p.CompanyID)); } db.Add(poco); key.AddKey(poco, model, model.GetName(p => p.CompanyID)); }