public static Concept ToDbModel(this Concept <decimal> concept, DisneyHCMLatamPlanningEntities context) { var conceptDb = context .Concepts .Include("Parameter1") .Include("Parameter2") .Include("Parameter3") .Include("Parameter4") .Include("Output1") .Include("Output2") .Include("ConceptFilters.Filter") .Include("ConceptFilters.Filter.Parameter") .Include("ConceptFilters.Filter.Parameter1") .FirstOrDefault(p => p.ConceptId == concept.Id); conceptDb = conceptDb ?? new Concept(); conceptDb.Scheme = context.Schemes.FirstOrDefault(s => s.SchemeId == concept.Scheme.Id); conceptDb.Description = concept.Description; conceptDb.Title = concept.Title; conceptDb.Sequence = concept.Sequence; conceptDb.Operation = concept.GetOperationText(); if (concept.GLAccount != null) { conceptDb.GLAccount = context.GLAccounts.FirstOrDefault(gl => gl.GLAccountId == concept.GLAccount.Id); } else { conceptDb.GLAccount = null; } #region Valid Months SyncValidMonths(conceptDb, concept, context); #endregion #region Filters conceptDb.ConceptFilters.ToList().ForEach(r => context.ConceptFilters.DeleteObject(r)); foreach (var filter in concept.ConceptFilters) { Filter <decimal, bool> filter1 = filter; var conceptFilter = new ConceptFilter(); conceptFilter.Filter = new Filter(); if (filter.Connector != null) { conceptFilter.Connector = new Operation().GetOperator(filter.Connector.GetOperator().ToString()); } conceptFilter.Sequence = filter.Sequence; conceptFilter.Filter.Description = filter.Description; conceptFilter.Filter.Operation = new Operation().GetOperator(filter.Operation.GetOperator().ToString()); conceptFilter.Filter.Title = filter.Name; conceptFilter.Filter.FilterType = context.FilterTypes.FirstOrDefault(ft => ft.FilterTypeId == filter1.FilterType); conceptFilter.Filter.Parameter = context.Parameters.FirstOrDefault(p => p.ParameterId == filter1.Parameter1.Id); if (filter.Parameter2.ParameterType == FromDisney.ParameterType.FixedValue) { Parameter parameter = null; if (filter.Parameter2.Id == 0) { parameter = context.Parameters.FirstOrDefault(x => x.FixedValue == filter1.Parameter2.FixedValue); if (parameter != null) { conceptFilter.Filter.Parameter1 = parameter; } else { conceptFilter.Filter.Parameter1 = new Parameter { Name = "CONSTANT_" + filter1.Parameter2.FixedValue.ToString(), Description = "Created by System", IsAccumulator = false, Company = conceptFilter.Filter.Parameter.Company }; } } else { conceptFilter.Filter.Parameter1 = context.Parameters.FirstOrDefault(p => p.ParameterId == filter1.Parameter2.Id); } conceptFilter.Filter.Parameter1.ParameterType = context.ParameterTypes.FirstOrDefault( pt => pt.ParameterTypeId == (int)filter1.Parameter2.ParameterType); conceptFilter.Filter.Parameter1.FixedValue = filter1.Parameter2.FixedValue; } else { conceptFilter.Filter.Parameter1 = context.Parameters.FirstOrDefault(p => p.ParameterId == filter1.Parameter2.Id); } conceptDb.ConceptFilters.Add(conceptFilter); } #endregion #region Input y Output Parameters conceptDb.Output1 = concept.Output1 != null?context.Parameters.FirstOrDefault(p => p.ParameterId == concept.Output1.Id) : null; conceptDb.Output2 = concept.Output2 != null?context.Parameters.FirstOrDefault(p => p.ParameterId == concept.Output2.Id) : null; conceptDb.Parameter1 = concept.Parameter1 != null?context.Parameters.FirstOrDefault(p => p.ParameterId == concept.Parameter1.Id) : null; conceptDb.Parameter2 = concept.Parameter2 != null?context.Parameters.FirstOrDefault(p => p.ParameterId == concept.Parameter2.Id) : null; conceptDb.Parameter3 = concept.Parameter3 != null?context.Parameters.FirstOrDefault(p => p.ParameterId == concept.Parameter3.Id) : null; conceptDb.Parameter4 = concept.Parameter4 != null?context.Parameters.FirstOrDefault(p => p.ParameterId == concept.Parameter4.Id) : null; #endregion if (concept.ExpenseType != null) { conceptDb.ExpenseType = context.ExpenseTypes.FirstOrDefault(et => et.ExpenseTypeId == concept.ExpenseType.Id); } return(conceptDb); }