public void Save()
        {
            using (var context = _repository.NameOrConnectionString == null ? new SqlContext() : new SqlContext(_repository.NameOrConnectionString))
            {
                var backup = new Backup
                                 {
                                     Name = Name = GetRandomName (),
                                     CreationDate = CreationDate,
                                     Creator = Creator,
                                     Description = Description,
                                     ParentSqlBackup = ParentBackup == null ? null : context.Backups.Single(a => a.Name == ParentBackup.Name),
                                     Blobs = new EntityCollection<Blob> (),
                                     DeletedFiles = new EntityCollection<DeletedFile> ()
                                 };

                foreach (var file in _files)
                {
                    var hashStream = new MemoryStream(file.Value.ToArray ());
                    var blob = new Blob
                                    {
                                        Backup = backup,
                                        Path = file.Key,
                                        Hash = FileHelpers.CalculateHash(hashStream),
                                    };
                    var blobData = new BlobData
                                       {
                                           Blob = blob,
                                           Data = hashStream.ToArray ()
                                       };
                    blob.BlobData = blobData;
                    backup.Blobs.Add(blob);
                    context.Blobs.Add(blob);
                    context.BlobDatas.Add(blobData);
                }
                foreach (var fileDeletion in _fileDeletions)
                {
                    var deletedFile = new DeletedFile
                                          {
                                              Backup = backup,
                                              Path = fileDeletion
                                          };
                    backup.DeletedFiles.Add(deletedFile);
                    context.DeletedFiles.Add(deletedFile);
                }

                context.Backups.Add(backup);

                context.SaveChanges ();
            }
        }
Exemple #2
0
 public EntityBackup(EntityRepository repository, int id)
 {
     _context = (_repository = repository).NameOrConnectionString != null ? new SqlContext(repository.NameOrConnectionString) : new SqlContext ();
     _backup = _context.Backups.Single(a => a.BackupId == id);
 }