public Tuple <bool, string> SaveToDatabase(string savedFilePath, AuthenticationType authenticationType, AuthenticationType userAuthenticationType, long levelId) { try { string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={savedFilePath};Extended Properties=Excel 12.0;"; int index = (savedFilePath).LastIndexOf('.'); int indexs = (savedFilePath).LastIndexOf('\\'); string name = (savedFilePath).Substring(indexs + 1); string sheet = (savedFilePath).Substring(indexs + 1, index - (indexs + 1)); using (OleDbConnection connection = new OleDbConnection(connectionString)) { using ( OleDbCommand cmd = new OleDbCommand("SELECT Code FROM [Sheet1$]", connection)) { connection.Open(); using (OleDbDataReader dReader = cmd.ExecuteReader()) { var lstExcel = new List <AuthenticationModel>(); while (dReader != null && dReader.Read()) { AuthenticationModel authentication = new AuthenticationModel { IdentityCode = Convert.ToString(dReader["Code"]), AuthenticationType = authenticationType }; lstExcel.Add(authentication); } var lstAllAuthentication = _authenticationRepository.All() .Select( s => new AuthenticationModel { IdentityCode = s.IdentityCode, AuthenticationType = s.AuthenticationType }) .ToList(); var lstNewAuthentication = lstExcel.Except(lstAllAuthentication, new AuthenticationComparer()).ToList(); var lstAuthen = new List <Authentication>(); foreach (var item in lstNewAuthentication) { Authentication authentication = new Authentication { IdentityCode = item.IdentityCode, AuthenticationType = item.AuthenticationType }; if (userAuthenticationType == AuthenticationType.AdminCentral) { authentication.CentralOrganizationId = 1; } else if (userAuthenticationType == AuthenticationType.AdminCentral) { authentication.BranchProvinceId = 1; } else { authentication.UniversityId = 1; } lstAuthen.Add(authentication); } // var lstAuthen = lstNewAuthentication.Select(item => new Authentication() // { // IdentityCode = item.IdentityCode, // AuthenticationType = item.AuthenticationType, // UniversityId = 1 //}).ToList(); lstAuthen.ForEach(b => _authenticationRepository.AddOrUpdate(b)); _unotOfWork.SaveChanges(); } } } return(new Tuple <bool, string>(true, "ثبت عملیات به درستی انجام شد")); } catch (Exception ex) { return(new Tuple <bool, string>(false, "خطا در ثبت عملیات")); } }