public string UpdateTableAndfieldconfiguration(TableAndFieldConfigurationServiceModel tableAndFieldConfiguration)
        {
            try
            {
                if (tableAndFieldConfiguration == null)
                {
                    //return Exception;
                }
                var dataToInsert            = new List <FieldConfiguration>();
                var dataToUpdate            = new List <FieldConfiguration>();
                var idsToDeleteFieldMapping = new List <FieldMappingConfiguration>();

                using (var repo = new RepositoryPattern <TableConfiguration>())
                {
                    var detailsToUpdate = Mapper.Map <TableConfiguration>(tableAndFieldConfiguration.tableConfiguration);
                    repo.Update(detailsToUpdate);
                    repo.Save();
                }
                tableAndFieldConfiguration.fieldConfiguration.ForEach(f =>
                                                                      { f.TableConfigId = tableAndFieldConfiguration.tableConfiguration.Id; f.IsDisplay = true; });

                dataToInsert = Mapper.Map <List <FieldConfiguration> >(tableAndFieldConfiguration.fieldConfiguration.Where(f => f.Id == default(int)).ToList());
                dataToUpdate = Mapper.Map <List <FieldConfiguration> >(tableAndFieldConfiguration.fieldConfiguration.Where(u => u.Id != default(int)).ToList());

                var configurationIds = dataToUpdate.Select(i => i.Id).ToList();

                using (var repo = new RepositoryPattern <FieldConfiguration>())
                {
                    //Update existing records which will not display in future
                    var records = repo.SelectAll().Where(f => f.TableConfigId == tableAndFieldConfiguration.tableConfiguration.Id &&
                                                         !configurationIds.Any(c => c == f.Id)).Select(c => { c.IsDisplay = false; return(c); }).ToList();

                    if (records.Any())
                    {
                        dataToUpdate.AddRange(records);
                    }

                    if (dataToUpdate.Any())
                    {
                        repo.BulkUpdate(dataToUpdate);
                    }
                    if (dataToInsert.Any())
                    {
                        repo.BulkInsert(dataToInsert);
                    }

                    idsToDeleteFieldMapping.AddRange(repo.SelectAll().Where(h => h.TableConfigId == tableAndFieldConfiguration.tableConfiguration.Id && h.FieldMappingConfigurations.Any())
                                                     .SelectMany(g => g.FieldMappingConfigurations).ToList());
                }
                tableAndFieldConfiguration.fieldConfiguration.ForEach(field =>
                {
                    if (field.Id == default(int))
                    {
                        var dataWithIds = dataToInsert.FirstOrDefault(a => a.SourceColumnName == field.SourceColumnName &&
                                                                      a.TableConfigId == field.TableConfigId && a.ReferenceTableName == field.ReferenceTableName &&
                                                                      a.ReferenceColumnName == field.ReferenceColumnName && field.IsDisplay).Id;
                        field.Id = dataWithIds;
                    }
                });

                List <FieldMappingConfiguration> fieldMappingConfigurations = new List <FieldMappingConfiguration>();
                tableAndFieldConfiguration.fieldConfiguration.ForEach(fields =>
                {
                    if (fields.MappedCoumns.Any())
                    {
                        fields.MappedCoumns.ForEach(m =>
                        {
                            var mappingDetails = new FieldMappingConfiguration()
                            {
                                FieldConfigurationId = fields.Id,
                                MapColumnName        = m,
                                MapTableName         = fields.ReferenceTableName,
                            };
                            fieldMappingConfigurations.Add(mappingDetails);
                        });
                    }
                });

                using (var repo = new RepositoryPattern <FieldMappingConfiguration>())
                {
                    if (idsToDeleteFieldMapping.Any())
                    {
                        repo.BulkDelete(idsToDeleteFieldMapping);
                    }
                    if (fieldMappingConfigurations.Any())
                    {
                        repo.BulkInsert(fieldMappingConfigurations);
                    }
                }

                return("Table and field mapping Save succesfully");
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public string SaveTableConfiguraionDetails(TableAndFieldConfigurationServiceModel tableAndFieldConfiguration)
        {
            try
            {
                if (tableAndFieldConfiguration == null)
                {
                    //return exce
                }
                if (!tableAndFieldConfiguration.fieldConfiguration.Any())
                {
                    //return
                }
                var tableConfigData = Mapper.Map <TableConfiguration>(tableAndFieldConfiguration.tableConfiguration);
                using (var tableConfigRepo = new RepositoryPattern <TableConfiguration>())
                {
                    tableConfigRepo.Insert(tableConfigData);
                    tableConfigRepo.Save();
                }

                //for entry in table
                tableAndFieldConfiguration.fieldConfiguration.ForEach(v => { v.TableConfigId = tableConfigData.Id; v.IsDisplay = true; });
                List <FieldConfiguration> fieldDetails = Mapper.Map <List <FieldConfiguration> >(tableAndFieldConfiguration.fieldConfiguration);
                using (var fieldConfigRepo = new RepositoryPattern <FieldConfiguration>())
                {
                    fieldConfigRepo.BulkInsert(fieldDetails);
                }
                List <FieldMappingConfiguration> fieldMappingConfigurations = new List <FieldMappingConfiguration>();
                tableAndFieldConfiguration.fieldConfiguration.ForEach(field =>
                {
                    var dataWithId = fieldDetails.FirstOrDefault(a => a.SourceColumnName == field.SourceColumnName &&
                                                                 a.TableConfigId == field.TableConfigId && a.ReferenceTableName == field.ReferenceTableName &&
                                                                 a.ReferenceColumnName == field.ReferenceColumnName).Id;
                    field.Id = dataWithId;
                });
                tableAndFieldConfiguration.fieldConfiguration.ForEach(field =>
                {
                    if (!string.IsNullOrWhiteSpace(field.ReferenceTableName) && !string.IsNullOrWhiteSpace(field.ReferenceColumnName))
                    {
                        if (field.MappedCoumns.Any())
                        {
                            field.MappedCoumns.ForEach(m =>
                            {
                                var mappingDetails = new FieldMappingConfiguration()
                                {
                                    FieldConfigurationId = field.Id,
                                    MapColumnName        = m,
                                    MapTableName         = field.ReferenceTableName,
                                };
                                fieldMappingConfigurations.Add(mappingDetails);
                            });
                        }
                    }
                });

                if (fieldMappingConfigurations.Any())
                {
                    using (var fieldMappingConfigRepo = new RepositoryPattern <FieldMappingConfiguration>())
                    {
                        fieldMappingConfigRepo.BulkInsert(fieldMappingConfigurations);
                    }
                }
                return("Table Configuration Saved Succefully");
            }
            catch (Exception ex)
            {
                throw;
            }
        }