/// <summary> /// Creates the record. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t">The t.</param> /// <returns></returns> public bool CreateRecord <T>(T t) { var retval = true; try { using (var context = new MetaFSDB()) { if (t is RegisteredPC) { context.RegisteredPCs.Add(t as RegisteredPC); } else if (t is FileCatalogue) { context.FileCatalogues.Add(t as FileCatalogue); } else if (t is Metadata) { context.Metadatas.Add(t as Metadata); } context.SaveChanges(); } } catch (Exception ex) { retval = false; Logger.LogError(ex); } return(retval); }
/// <summary> /// Initializes the database. /// </summary> private void InitializeDb() { try { using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { using (var context = new MetaFSDB()) { // Create basic categories if (!context.Categories.Any()) { context.Categories.AddRange(GetBasicCategories()); } // Create basic object types if (!context.ObjectTypes.Any()) { context.ObjectTypes.AddRange(GetBasicObjectTypes()); } // Register PC SelectedMac = NetworkOps.GetMacAddress(); var registeredPc = context.RegisteredPCs.FirstOrDefault(x => x.PCName.Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase)); if (registeredPc == null) { registeredPc = new RegisteredPC() { MacAddress = SelectedMac, IsEnabled = true, PCName = Environment.MachineName }; context.RegisteredPCs.Add(registeredPc); } else { registeredPc.MacAddress = SelectedMac; context.RegisteredPCs.Attach(registeredPc); var entry = context.Entry(registeredPc); entry.Property(e => e.MacAddress).IsModified = true; } context.SaveChanges(); PcIdentifier = registeredPc.PCId; MachineName = registeredPc.PCName; } ts.Complete(); } } catch (Exception ex) { Logger.LogError(ex); } }
/// <summary> /// Updates the file metadata. /// </summary> /// <param name="currentMetadata">The current metadata.</param> public void UpdateFileMetadata(IFileMetadata currentMetadata) { var metadata = GetMetadataAsXml(Renderer.RehydrateFromUi(), currentMetadata); try { using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { using (var context = new MetaFSDB()) { if (currentMetadata.MetadataId != 0) { UpdateExistingRecord(currentMetadata, context, metadata); } else { var newFileEntry = context.FileCatalogues.Add(new FileCatalogue() { Fk_PCId = PcIdentifier, FileName = currentMetadata.FileName, FullPath = currentMetadata.FullPath, Fk_ObjectType = 1 }); context.SaveChanges(); context.Metadatas.Add(new Metadata() { Fk_RowId = newFileEntry.RowId, Fk_TemplateId = currentMetadata.TemplateId, Metadata1 = metadata.ToString() }); context.SaveChanges(); } } ts.Complete(); } } catch (Exception ex) { Logger.LogError(ex); } }
/// <summary> /// Updates the existing record. /// </summary> /// <param name="currentMetadata">The current metadata.</param> /// <param name="context">The context.</param> /// <param name="metadata">The metadata.</param> private void UpdateExistingRecord(IFileMetadata currentMetadata, MetaFSDB context, XDocument metadata) { var existing = context.Metadatas.FirstOrDefault(x => x.MetaId == currentMetadata.MetadataId); existing.Metadata1 = metadata.ToString(); context.Metadatas.Attach(existing); var entry = context.Entry(existing); entry.Property(e => e.Metadata1).IsModified = true; context.SaveChanges(); }