private static void PersistRows(List <Models.ColoradoBusiness> rows, int batchSize, ref int recordsAdded) { using (var context = new BusinessIncentivesContext()) { try { context.ColoradoBusinesses.AddRange(rows); context.SaveChanges(); } catch { if (rows.Count == 1) { recordsAdded--; return; } for (int i = 0; i < rows.Count; i++) { var temp = new List <Models.ColoradoBusiness>(); temp.Add(rows[i]); PersistRows(temp, 1, ref recordsAdded); } } } }
private static void PersistPrograms(List <Models.Program> programs, int batchSize) { using (var context = new BusinessIncentivesContext()) { context.Programs.AddRange(programs); context.SaveChanges(); } }
public UserCreationResult CreateUser(UserModel userModel, byte[] hash, byte[] salt) { var result = new UserCreationResult(); if (userModel == null) { throw new ArgumentNullException("userModel"); } if (hash.Length == 0) { throw new ArgumentException("Hash can't be empty"); } try { using (var context = new BusinessIncentivesContext()) { IQueryable <User> existingUser = context.Users.Where(o => o.Username == userModel.Username || o.Email == userModel.Email); if (existingUser.Count() > 0) { result.IsSuccess = false; result.ErrorMessage = "User already exists!"; } else { var user = new User() { Username = userModel.Username, Password = hash, Email = userModel.Email, FirstName = userModel.FirstName, LastName = userModel.LastName, Salt = salt, PermissionLevelID = (short)PermissionLevels.OpportunityCreator }; user = context.Users.Add(user); context.SaveChanges(); result.UserCreated = user; result.IsSuccess = true; } } } catch (Exception ex) { result.IsSuccess = false; result.ErrorMessage = ex.ToString(); } return(result); }
public long?AddCustomOpportunity(CustomOpportunity customOpportunity) { using (var context = new BusinessIncentivesContext()) { IQueryable <CustomOpportunity> query = context.CustomOpportunities.Where(o => o.Name == customOpportunity.Name); var result = query?.ToList(); if (result?.Count > 0) { return(null); } context.CustomOpportunities.Add(customOpportunity); context.SaveChanges(); query = context.CustomOpportunities.Where(o => o.Name == customOpportunity.Name); result = query?.ToList(); return(result[0]?.ID); } }
public DataLinkResult LinkData() { Console.CursorVisible = false; Console.WriteLine($"Creating cross reference data between Opportunities and Programs..."); // Get the list of Opportunity record ID's to process var opportunityIDs = new List <string>(); using (var context = new BusinessIncentivesContext()) { // We get the IDs from OpportunityView instead of Opportunity, // which filters the opportunities still available only... opportunityIDs = context.OpportunityViews.Select(o => o.OpportunityID).ToList(); } // Loop through 100 Opportunity records, and for // each get and attach the related programs var batchSize = 100; var counter = 0; var cursorTop = Console.CursorTop + 1; using (var context = new BusinessIncentivesContext()) { foreach (var opportunityID in opportunityIDs) { var opportunity = context.Opportunities.Single(o => o.OpportunityID == opportunityID); var relatedPrograms = context.Programs.Where(p => opportunity.CFDANumbers.Contains(p.ProgramNumber)); string message = string.Empty; var areProgramsAlreadyAttached = opportunity.Programs != null && opportunity.Programs.Count() > 0; if (areProgramsAlreadyAttached) { message = $"Opportunity ID {opportunity.OpportunityID} has its related Programs already attached"; } else { opportunity.Programs = relatedPrograms.ToList(); var programCount = relatedPrograms.Count(); message = $"Opportunity ID {opportunity.OpportunityID} has {programCount} related Program{(programCount > 1 ? "(s)" : "")}"; } Console.WriteLine(string.Join(" ", Enumerable.Range(0, 50).Select(i => " "))); Console.SetCursorPosition(0, cursorTop + 1); Console.WriteLine(message); counter++; if (counter % batchSize == 0 || counter == opportunityIDs.Count) { // Persist batch context.SaveChanges(); Console.SetCursorPosition(0, cursorTop); Console.WriteLine($"Count of Opportunity records processed: {counter} / {opportunityIDs.Count}"); } } Console.SetCursorPosition(0, cursorTop + 1); } return(new DataLinkResult { CountOfRecordsLinked = counter, IsSuccess = true }); }