public void LoaderContext_Save_CanSave() { var repo = new DimRepo { Name = "CodeMetricsLoader" }; var commit = new DimCommit { Sha = "9c4800fdeb47aa8f990105fd894ab1f125efc51e", Url = "https://api.github.com/repos/StanBPublic/CodeMetricsLoader/commits/9c4800fdeb47aa8f990105fd894ab1f125efc51e", Committer = "JoeDoe", Date = DateTime.Now, }; repo.Commits.Add(commit); _context.Repos.Add(repo); var date = new DimDate(); var churn = new FactCodeChurn { Commit = commit, Date = date, LinesAdded = 10, LinesDeleted = 2, TotalChurn = 12 }; _context.Churn.Add(churn); _context.SaveChanges(); }
/// <summary> /// Saves commits for the given rep /// </summary> /// <param name="repoName">Name of the repository</param> /// <param name="commits">Commit list</param> public void SaveCommits(DateTime runDate, string repoName, List <Commit> commits) { if (HaveDataForThisDate(repoName, runDate)) { _logger.Log("Already have data for this repo and date."); return; } _logger.Log("Saving to database..."); var dimDate = new DimDate(runDate); dimDate = GetOrAddEntity <DimDate>(_context.Dates, dimDate, delegate(DimDate d) { return(d.Date == dimDate.Date); }, true); var repo = new DimRepo { Name = repoName }; repo = GetOrAddEntity <DimRepo>(_context.Repos, repo, delegate(DimRepo r) { return(r.Name == repo.Name); }, true); FactCodeChurn churn; foreach (var commit in commits) { DimCommit dimCommit = Mapper.Map <DimCommit>(commit); dimCommit.Repo = repo; _context.Commits.Add(dimCommit); churn = Mapper.Map <FactCodeChurn>(commit); churn.Commit = dimCommit; churn.Date = dimDate; _context.Churn.Add(churn); foreach (var file in commit.Files) { DimFile dimFile = Mapper.Map <DimFile>(file); dimFile = GetOrAddEntity <DimFile>(_context.Files, dimFile, delegate(DimFile f) { return(f.FileName == dimFile.FileName); }, false); dimFile.Commits.Add(dimCommit); churn = Mapper.Map <FactCodeChurn>(file); churn.File = dimFile; churn.Date = dimDate; _context.Churn.Add(churn); } try { _context.SaveChanges(); } catch (DbEntityValidationException ex) { var sb = new StringBuilder("Failed to save\n"); foreach (var entity in ex.EntityValidationErrors) { sb.AppendLine(string.Format("Entity: {0}", entity.Entry.Entity)); foreach (var error in entity.ValidationErrors) { sb.AppendLine(string.Format("Property: {0}", error.PropertyName)); sb.AppendLine(string.Format("Error: {0}", error.ErrorMessage)); } sb.AppendLine(); } throw new ApplicationException(sb.ToString(), ex); } } _logger.Log("Done."); }