public async Task DeleteProject(Guid projectId)
 {
     using (var audioViewEntities = new AudioViewEntities())
     {
         var project = await audioViewEntities.Projects.Where(x => x.Id == projectId).FirstOrDefaultAsync().ConfigureAwait(false);
         if (project == null)
             return;
         audioViewEntities.Projects.Remove(project);
         audioViewEntities.SaveChanges();
     }
 }
 public async Task DeleteReading(Guid readingId)
 {
     using (var audioViewEntities = new AudioViewEntities())
     {
         var reading = await audioViewEntities.Readings.Where(x => x.Id == readingId).FirstOrDefaultAsync().ConfigureAwait(false);
         if (reading == null)
             return;
         audioViewEntities.Readings.Remove(reading);
         audioViewEntities.SaveChanges();
     }
 }
        public static Task UploadLocalFiles()
        {
            return Task.Run(() =>
            {
                lock (LogFileLock)
                {
                    DirectoryInfo directory = new DirectoryInfo(DataFolder);
                    logger.Trace("Looking or unuploade files in \"{0}\".", directory.FullName);
                    if (!directory.Exists)
                    {
                        logger.Trace("No files where found in \"{0}\".", directory.FullName);
                        return;
                    }

                    List<string> filesToDelete = new List<string>(); 
                    foreach (var fileInfo in directory.GetFiles())
                    {
                        try
                        {
                            logger.Info("Found file to upload \"{0}\"", fileInfo.FullName);
                            var lines = File.ReadAllLines(fileInfo.FullName);
                            var settings = JsonConvert.DeserializeObject<MeasurementSettingsDataStorageWarpper>(lines[0]);
                            var readings =
                                lines.Skip(1)
                                    .Select(JsonConvert.DeserializeObject<ReadingDataDataStorageWarpper>)
                                    .ToList();
                            logger.Debug("File had {0} readings.", readings.Count);
                            
                            using (var audioViewEntities = new AudioViewEntities())
                            {
                                if (!audioViewEntities.Projects.Any(x => x.Id == settings.Id))
                                {
                                    logger.Debug("Project \"{0}\" was not in the database adding.", settings.MeasurementSettings.ProjectName);
                                    audioViewEntities.Projects.Add(new Project()
                                    {
                                        Id = settings.Id,
                                        Created = settings.Created,
                                        MinorDBLimit = settings.MeasurementSettings.MinorDBLimit,
                                        MajorDBLimit = settings.MeasurementSettings.MajorDBLimit,
                                        MajorInterval = settings.MeasurementSettings.MajorInterval.Ticks,
                                        MinorInterval = settings.MeasurementSettings.MinorInterval.Ticks,
                                        Name = settings.MeasurementSettings.ProjectName,
                                        Number = settings.MeasurementSettings.ProjectNumber
                                    });
                                    audioViewEntities.SaveChanges();
                                }

                                var knownReadings =
                                    audioViewEntities.Readings.Where(x => x.Project == settings.Id)
                                        .Select(x => x.Id)
                                        .ToList();
                                logger.Trace("Got {0} known readings for {1}", knownReadings.Count, settings.Id);
                                var readingsToUpload = readings.Where(x => !knownReadings.Contains(x.Id)).ToList();
                                foreach (var r in readingsToUpload)
                                {
                                    audioViewEntities.Readings.Add(new Reading()
                                    {
                                        Id = r.Id,
                                        Project = settings.Id,
                                        Major = r.IsMajor ? (byte) 1 : (byte) 0,
                                        Time = r.Time,
                                        LAeq = r.Data.LAeq,
                                    });
                                }
                                logger.Trace("Added {0} new readings for {1}", readingsToUpload.Count, settings.Id);
                                audioViewEntities.SaveChanges();
                            }
                            // If we got to here is the file uploaded
                            filesToDelete.Add(fileInfo.FullName);
                        }
                        catch (DbEntityValidationException e)
                        {
                            logger.Error(e, "Was unable to uploade \"{0}\"", fileInfo.FullName);
                            foreach (var eve in e.EntityValidationErrors)
                            {
                                logger.Error("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                foreach (var ve in eve.ValidationErrors)
                                {
                                    logger.Error("- Property: \"{0}\", Error: \"{1}\"",
                                        ve.PropertyName, ve.ErrorMessage);
                                }
                            }
                            throw;
                        }
                        catch (Exception exp)
                        {
                            logger.Error(exp, "Was unable to uploade \"{0}\"", fileInfo.FullName);
                        }
                    }

                    foreach (var f in filesToDelete)
                    {
                        try
                        {
                            new FileInfo(f).Delete();
                        }
                        catch (Exception exp)
                        {
                            logger.Error(exp, "Was unable to delete \"{0}\" all data have been uploaded.", f);
                        }
                    }
                }
            });
        }