/// <summary> /// Delete a single greenhouse /// </summary> /// <param name="dc"></param> /// <param name="greenhouse"></param> public void Delete(DataContext dc, Domain.Greenhouse greenhouse) { dc = dc ?? Conn.GetContext(); var dbGreenhouse = dc.Greenhouses.Where(g => g.GreenhouseID == greenhouse.ID).SingleOrDefault(); if (dbGreenhouse == null) { return; } //dc.Greenhouses.Attach(dbGreenhouse, true); foreach (var section in dbGreenhouse.Sections) { dc.Sections.DeleteOnSubmit(section); foreach (var task in section.Tasks) { dc.Tasks.DeleteOnSubmit(task); } } dc.Greenhouses.DeleteOnSubmit(dbGreenhouse); dc.SubmitChanges(); }
/// <summary> /// Save a Greenhouse /// </summary> /// <param name="dc"></param> /// <param name="greenhouse"></param> /// <returns>returns the id of the saved greenhouse</returns> public int Save(DataContext dc, Domain.Greenhouse greenhouse) { dc = dc ?? Conn.GetContext(); var dbGreenhouse = dc.Greenhouses.Where(g => g.GreenhouseID == greenhouse.ID).SingleOrDefault(); var isNew = false; if (dbGreenhouse == null) { dbGreenhouse = new DataAccess.SqlRepository.Greenhouse(); isNew = true; } dbGreenhouse.City = greenhouse.Address.City; dbGreenhouse.Country = greenhouse.Address.Country; dbGreenhouse.StateOrProvince = greenhouse.Address.StateOrProvince; dbGreenhouse.Zip = greenhouse.Address.Zip; dbGreenhouse.StreetLine1 = greenhouse.Address.StreetLine1; dbGreenhouse.StreetLine2 = greenhouse.Address.StreetLine2; dbGreenhouse.IsDefault = greenhouse.Address.IsDefault; dbGreenhouse.DateUpdated = DateTime.Now; if (isNew) { dbGreenhouse.DateCreated = DateTime.Now; dc.Greenhouses.InsertOnSubmit(dbGreenhouse); } dc.SubmitChanges(); greenhouse.ID = dbGreenhouse.GreenhouseID; if (greenhouse.Sections != null) { foreach (Section section in greenhouse.Sections) { section.Save(); } } return(greenhouse.ID); }