/// <summary> /// Saves documents from the specified composite contract to the Document Library. /// </summary> /// <param name="composite">The EnterpriseScenarioCompositeContract.</param> /// <returns>true if the operation completed successfully.</returns> public static bool ProcessCompositeContractFile(EnterpriseScenarioCompositeContract composite) { using (DocumentLibraryContext context = DbConnect.DocumentLibraryContext()) { bool changesMade = false; foreach (DocumentContract documentContract in composite.Documents) { if (CanInsertDocument(documentContract, context)) { TestDocument documentEntity = ContractFactory.Create(context, documentContract); WriteDocumentToServer(documentEntity, GlobalSettings.Items[Setting.DocumentLibraryServer], documentContract.Data); context.TestDocuments.Add(documentEntity); changesMade = true; } } if (changesMade) { context.SaveChanges(); } } return(true); }
public static void Seed(IConfigurationRoot config, string connectionStringName, IUserService userService) { DocumentLibraryContext dbContext = BuildDbContext(config, connectionStringName, userService); dbContext.Database.EnsureCreated(); if (dbContext.Books.Any()) { return; } ; var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(dbContext), null, null, null, null); SeedRole(roleManager, UserRoles.Admin); SeedRole(roleManager, UserRoles.StandardUser); var adminUser = new ApplicationUser { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, FirstName = "John", LastName = "Doe" }; string adminUserPassword = "******"; var standardUser = new ApplicationUser { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, FirstName = "Richard", LastName = "Roe" }; string standardUserPassword = "******"; var optionsInstance = new IdentityOptions { Password = { RequireDigit = true } }; IOptions <IdentityOptions> optionParameter = Options.Create(optionsInstance); var userManager = new UserManager <ApplicationUser>( new UserStore <ApplicationUser>(dbContext), optionParameter, new PasswordHasher <ApplicationUser>(), null, null, null, null, null, null); SeedUser(userManager, adminUser, adminUserPassword, UserRoles.Admin); SeedUser(userManager, standardUser, standardUserPassword, UserRoles.StandardUser); var sciFiGenre = new Genre { Name = "Sci-Fi" }; var fictionGenre = new Genre { Name = "Fiction" }; var popularScience = new Genre { Name = "Popular science" }; var tragedy = new Genre { Name = "Tragedy" }; var books = new List <Book> { new Book { Name = "Space Odyssey", Description = "One of the best sci-fi books in the world", Genre = sciFiGenre, Keywords = new List <Keyword> { new Keyword { Name = "adventure" }, new Keyword { Name = "space" }, new Keyword { Name = "artificial intelligence" }, new Keyword { Name = "astronaut" } }, PublicationDate = new DateTime(1968, 8, 23), BookCheckouts = new List <BookCheckout> { new BookCheckout { ApplicationUser = standardUser, AvailabilityDate = DateTime.Now.AddDays(5) } } }, new Book { Name = "Gone with the wind", Description = "One of the top fiction books of the 20th century", Genre = fictionGenre, Keywords = new List <Keyword> { new Keyword { Name = "crime" }, new Keyword { Name = "drama" } }, PublicationDate = new DateTime(1936, 6, 30) }, new Book { Name = "Brief history of time", Genre = popularScience, Keywords = new List <Keyword> { new Keyword { Name = "science" }, new Keyword { Name = "theoretical physics" }, new Keyword { Name = "quantum mechanics" } }, PublicationDate = new DateTime(1988, 7, 29) }, new Book { Name = "The Great Gatsby", Description = "The Great Gatsby provides a critical social history of Prohibition-era America during the Jazz Age", Genre = tragedy, Keywords = new List <Keyword> { new Keyword { Name = "tragedy" }, new Keyword { Name = "jazz era" } }, PublicationDate = new DateTime(1925, 11, 16) } }; dbContext.Books.AddRange(books); dbContext.SaveChanges(); }
public void SaveChanges() => _context.SaveChanges();
/// <summary> /// Commits this instance. /// </summary> private void Commit() { using (new BusyCursor()) { try { if (_deletedItems.Count > 0) { foreach (var item in _deletedItems) { _context.TestDocuments.Remove(item); } _context.SaveChanges(); _deletedItems.Clear(); } foreach (var document in _documents) { if (_context.Entry(document).State == EntityState.Added) { document.SubmitDate = DateTime.Now; _context.TestDocuments.Add(document); if (_addedDocumentPath.ContainsKey(document.TestDocumentId)) { var source = _addedDocumentPath[document.TestDocumentId]; try { ImportExportUtil.CopyDocumentToServer(document, GlobalSettings.Items[Setting.DocumentLibraryServer], source); TraceFactory.Logger.Debug("File copied"); _addedDocumentPath.Remove(document.TestDocumentId); } catch (UnauthorizedAccessException) { MessageBox.Show ( "You do not have authorization to save {0} to the STB Document Server. This document will not be added.".FormatWith(source), "Unauthorized Access", MessageBoxButtons.OK, MessageBoxIcon.Error ); } catch (Exception ex) { MessageBox.Show ( "A error occurred when trying to save {0} to the STB Document Server. This document will not be added. Error: {1}".FormatWith(source, ex.Message), "Unauthorized Access", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } else if (_importedDocumentData.ContainsKey(document.TestDocumentId)) { var data = _importedDocumentData[document.TestDocumentId]; ImportExportUtil.WriteDocumentToServer(document, GlobalSettings.Items[Setting.DocumentLibraryServer], data); TraceFactory.Logger.Debug("File imported"); _importedDocumentData.Remove(document.TestDocumentId); } } } _context.SaveChanges(); } catch (Exception ex) { TraceFactory.Logger.Error(ex); MessageBox.Show ( "A error occurred when trying to save to the STB Document Server. This document will not be added. Error: {0}".FormatWith(ex.Message), "Unauthorized Access", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } }