public static Sector Save(Sector sector) { using (var context = new TSMContext()) { if (context.Sectors.Where(v => v.ID == sector.ID).Count() > 0) { var sector_language = context.Sector_Languages.Where(sl => sl.ID == sector.ID).FirstOrDefault(); if (sector_language != null) { context.Sector_Languages.Remove(sector_language); } context.Sector_Languages.AddRange(sector.Sector_Languages); context.Sectors.Attach(sector); context.Entry(sector).State = EntityState.Modified; } else { context.Sectors.Add(sector); } //Save to DB context.SaveChanges(); return(sector); } }
/// <summary> /// Save XML Data for record download /// </summary> /// <param name="user"></param> /// <returns></returns> public static bool SaveXMLToData(ExportRecordXML xml) { try { using (var context = new TSMContext()) { //var xmlRecordID = context.ExportRecordXML.Select(a => a.ID).FirstOrDefault(); //context.ExportRecordXML.D.Delete(w => w.ID == xmlRecordID); var all = from c in context.ExportRecordXML select c; context.ExportRecordXML.RemoveRange(all); //context.Entry(xml).State = EntityState.Deleted; context.ExportRecordXML.Add(xml); //Save to DB context.SaveChanges(); return(true); } } catch (Exception) { return(false); } }
/// <summary> /// Save Page Content /// </summary> /// <param name="content"></param> /// <param name="languageID"></param> /// <returns></returns> public static Content Save(Content content, Guid languageID) { using (var context = new TSMContext()) { //Check if content exists or not if (context.Contents.Where(c => c.ID == content.ID).Count() > 0) { var content_language = context.Content_Languages.Where(cl => cl.ID == content.ID && cl.LanguageID == languageID).FirstOrDefault(); if (content_language != null) { context.Content_Languages.Remove(content_language); } context.Content_Languages.AddRange(content.Content_Languages); context.Contents.Attach(content); context.Entry(content).State = EntityState.Modified; } else { context.Contents.Add(content); } //Save to DB context.SaveChanges(); return(content); } }
/// <summary> /// Add or update content /// </summary> /// <param name="content"></param> /// <returns></returns> public static Content Save(Content content) { using (var context = new TSMContext()) { if (context.Contents.Where(c => c.ID == content.ID).Count() > 0) { var content_language = context.Content_Languages.Where(sl => sl.ID == content.ID).FirstOrDefault(); var ID = context.Content_Languages.Where(sl => sl.ID == content.ID).Count(); if (content_language != null) { context.Content_Languages.Remove(content_language); //context.SaveChanges(); //context.Entry(content).State = EntityState.Deleted; } context.Content_Languages.AddRange(content.Content_Languages); context.Contents.Attach(content); context.Entry(content).State = EntityState.Modified; } else { context.Contents.Add(content); } //Save DB context.SaveChanges(); return(content); } }
/// <summary> /// Delete TSM SECTOR /// </summary> /// <param name="tsmSector"></param> public static void Delete(TSMSector tsmSector) { using (var context = new TSMContext()) { context.TSMSector.Attach(tsmSector); context.Entry(tsmSector).Property(u => u.IsActive).IsModified = true; context.Entry(tsmSector).Property(u => u.IsDeleted).IsModified = true; context.Entry(tsmSector).Property(u => u.LastModifiedDate).IsModified = true; context.Entry(tsmSector).Property(u => u.LastModifiedBy).IsModified = true; context.SaveChanges(); } }
public static void Delete(Record record) { using (var context = new TSMContext()) { context.Records.Attach(record); context.Entry(record).Property(u => u.IsActive).IsModified = true; context.Entry(record).Property(u => u.IsDeleted).IsModified = true; context.Entry(record).Property(u => u.LastModifiedDate).IsModified = true; context.Entry(record).Property(u => u.LastModifiedBy).IsModified = true; context.SaveChanges(); } }
/// <summary> /// Save Variable /// </summary> /// <param name="user"></param> /// <returns></returns> public static Variable Save(Variable variable, Guid langID) { using (var context = new TSMContext()) { //Check if variable exists or not if (context.Variables.Where(v => v.ID == variable.ID).Count() > 0) { var variable_lang = context.Variable_Languages.Where(cl => cl.ID == variable.ID).FirstOrDefault(); var variable_choice = context.Choices.Include("Choice_Languages").Where(c => c.VariableID == variable.ID).ToList(); if (variable_lang != null) { context.Variable_Languages.Remove(variable_lang); } if (variable_choice != null) { foreach (var item in variable_choice) { Choice_Language itemChoiceLang = item.Choice_Languages.Where(c => c.LanguageID == langID).FirstOrDefault(); if (itemChoiceLang != null) { context.Choice_Languages.Remove(itemChoiceLang); context.Choices.Remove(item); } } } context.Choices.AddRange(variable.Choices); context.Variable_Languages.AddRange(variable.Variable_Languages); context.Variables.Attach(variable); context.Entry(variable).State = EntityState.Modified; } else { context.Variables.Add(variable); } //Save to DB try { context.SaveChanges(); } catch // (Exception ee) { // ErrorLog.WriteLog("VariableService", "Save", ee, ""); } return(variable); } }
/// <summary> /// Save Sector /// </summary> /// <param name="user"></param> /// <returns></returns> public static TSMSector Save(TSMSector tsmsSctor) { using (var context = new TSMContext()) { if (context.TSMSector.Where(v => v.ID == tsmsSctor.ID).Count() > 0) { context.TSMSector.Attach(tsmsSctor); context.Entry(tsmsSctor).State = EntityState.Modified; } else { context.TSMSector.Add(tsmsSctor); } //Save to DB context.SaveChanges(); return(tsmsSctor); } }
/// <summary> /// Add or update content /// </summary> /// <param name="content"></param> /// <returns></returns> public static Content Save(Content content) { using (var context = new TSMContext()) { if (context.Contents.Where(c => c.ID == content.ID).Count() > 0) { context.Contents.Attach(content); context.Entry(content).State = EntityState.Modified; } else { context.Contents.Add(content); } //Save DB context.SaveChanges(); return(content); } }
/// <summary> /// Save User /// </summary> /// <param name="user"></param> /// <returns></returns> public static User Save(User user) { using (var context = new TSMContext()) { //Check if user exists or not if (context.Users.Where(u => u.ID == user.ID).Count() > 0) { context.Users.Attach(user); context.Entry(user).State = EntityState.Modified; } else { context.Users.Add(user); } //Save to DB context.SaveChanges(); return(user); } }
/// <summary> /// Save Role /// </summary> /// <param name="role"></param> /// <returns></returns> public static Role Save(Role role) { using (var context = new TSMContext()) { //Check if user exists or not if (context.Roles.Where(r => r.ID == role.ID).Count() > 0) { context.Roles.Attach(role); context.Entry(role).State = EntityState.Modified; } else { context.Roles.Add(role); } //Save to DB context.SaveChanges(); return(role); } }
/// <summary> /// Save record /// </summary> /// <param name="record"></param> /// <param name="languageID"></param> /// <returns></returns> public static Record Save(Record record, Guid languageID, List <Guid> deletedDocuments, List <Guid> selectedCountries, List <Guid> selectedRegions, List <Guid> selectedSectors, List <Document> newDocuments) { using (var context = new TSMContext()) { //Check if record exists or not if (context.Records.Any(r => r.ID == record.ID)) // update { var tempRecord = context.Records.Where(r => r.ID == record.ID).FirstOrDefault(); tempRecord.LastModifiedBy = record.LastModifiedBy; tempRecord.LastModifiedDate = DateTime.UtcNow; tempRecord.IsActive = record.IsActive; tempRecord.IsPublic = record.IsPublic; //For Language var record_language = context.Record_Languages.Where(rl => rl.ID == record.ID && rl.LanguageID == languageID).FirstOrDefault(); if (record_language != null) { context.Record_Languages.Remove(record_language); } //For country, region, sector, variable var recordCountries = tempRecord.Countries.ToList(); recordCountries.ForEach(rc => tempRecord.Countries.Remove(rc)); var recordRegions = tempRecord.Regions.ToList(); recordRegions.ForEach(rr => tempRecord.Regions.Remove(rr)); var recordSectors = tempRecord.Sectors.ToList(); recordSectors.ForEach(rs => tempRecord.Sectors.Remove(rs)); var recordVariables = context.Record_Variables.Where(rv => rv.RecordID == tempRecord.ID).ToList(); recordVariables.ForEach(rv => context.Record_Variables.Remove(rv)); var countries = context.Countries.Include("Regions").Where(c => selectedCountries.Any(sc => sc == c.ID)); tempRecord.Countries.AddRange(countries); //auto add regions start selectedRegions = new List <Guid>(); foreach (var countryId in selectedCountries) { var tmpCountry = countries.Where(c => c.ID == countryId).FirstOrDefault(); if (tmpCountry != null) { foreach (var region in tmpCountry.Regions) { if (!selectedRegions.Any(r => r == region.ID)) { selectedRegions.Add(region.ID); } } } } //auto add regions end tempRecord.Regions.AddRange(context.Regions.Where(r => selectedRegions.Any(sr => sr == r.ID))); tempRecord.Sectors.AddRange(context.Sectors.Where(s => selectedSectors.Any(ss => ss == s.ID))); //For document delete (deleted files) var removedDocuments = context.Documents.Where(d => deletedDocuments.Any(dd => dd == d.ID)); context.Documents.RemoveRange(removedDocuments); tempRecord.Documents.RemoveAll(d => deletedDocuments.Any(dd => dd == d.ID)); //add new documents if (tempRecord.Documents == null) { tempRecord.Documents = new List <Document>(); } if (newDocuments != null && newDocuments.Count > 0) { tempRecord.Documents.AddRange(newDocuments); } tempRecord.Record_Languages = new List <Record_Language>(); tempRecord.Record_Languages.Add(record.Record_Languages[0]); tempRecord.Variables = new List <Record_Variable>(); tempRecord.Variables = record.Variables; context.Records.Attach(tempRecord); context.Entry(tempRecord).State = EntityState.Modified; } else // Add { var countries = context.Countries.Include("Regions").Where(c => selectedCountries.Any(sc => sc == c.ID)).ToList(); record.Countries.AddRange(countries); //auto add regions start selectedRegions = new List <Guid>(); foreach (var countryId in selectedCountries) { var tmpCountry = countries.Where(c => c.ID == countryId).FirstOrDefault(); if (tmpCountry != null) { foreach (var region in tmpCountry.Regions) { if (!selectedRegions.Any(r => r == region.ID)) { selectedRegions.Add(region.ID); } } } } //auto add regions end record.Regions.AddRange(context.Regions.Where(r => selectedRegions.Any(sr => sr == r.ID))); record.Sectors.AddRange(context.Sectors.Where(s => selectedSectors.Any(ss => ss == s.ID))); //Needed only for Data Import //record.Documents.AddRange(newDocuments); context.Records.Add(record); } //Save to DB context.SaveChanges(); return(record); } }