public async Task <int?> UpsertAsync(ServiceModel.RepositoryCurrentState repositoryCurrentState)
        {
            var dbRepositoryCurrentState = await repositoryAnalysisContext
                                           .RepositoryCurrentState
                                           .Include(rcs => rcs.Teams)
                                           .Include(rcs => rcs.Topics)
                                           .Where(rcs => rcs.RepositoryId == repositoryCurrentState.Id)
                                           .SingleOrDefaultAsync();

            if (dbRepositoryCurrentState == null)
            {
                dbRepositoryCurrentState = mapper.Map <RepositoryCurrentState>(repositoryCurrentState);

                repositoryAnalysisContext.Add(dbRepositoryCurrentState);
            }
            else
            {
                // If the object already exists in the DB, then map the model object into this
                // already existing db objec to take advantage of EF update tracking
                mapper.Map(repositoryCurrentState, dbRepositoryCurrentState);
            }

            await repositoryAnalysisContext.SaveChangesAsync();

            return(dbRepositoryCurrentState.RepositoryCurrentStateId);
        }
        public async Task UpsertAsync(ServiceModel.RepositorySnapshot snapshot, int?repositoryCurrentStateId = null)
        {
            var dbRepositoryCurrentState = await repositoryAnalysisContext
                                           .RepositoryCurrentState
                                           .Include(rcs => rcs.RepositorySnapshots)
                                           .ThenInclude(rs => rs.Dependencies)
                                           .Include(rcs => rcs.RepositorySnapshots)
                                           .ThenInclude(rs => rs.Files)
                                           .Include(rcs => rcs.RepositorySnapshots)
                                           .ThenInclude(rs => rs.TypesAndImplementations)
                                           .ThenInclude(rti => rti.Implementations)
                                           .Where(rcs => rcs.RepositorySnapshots.Any(rs => rs.WindowStartCommitId == snapshot.WindowStartCommitId))
                                           .SingleOrDefaultAsync();

            if (dbRepositoryCurrentState == null)
            {
                if (!repositoryCurrentStateId.HasValue || repositoryCurrentStateId.Value == 0)
                {
                    throw new ArgumentException("Unable to insert new Repository Snapshot without the Current State Id");
                }

                var dbSnapshot = mapper.Map <RepositorySnapshot>(snapshot);
                dbSnapshot.RepositoryCurrentStateId = repositoryCurrentStateId.Value;

                repositoryAnalysisContext.Add(dbSnapshot);
            }
            else
            {
                var dbSnapshot = dbRepositoryCurrentState.RepositorySnapshots.First();

                //If the object already exists in the DB, then map the model object into this
                // already existing db objec to take advantage of EF update tracking
                mapper.Map(snapshot, dbSnapshot);
            }

            await repositoryAnalysisContext.SaveChangesAsync();
        }