public SingleResult <ExpenseReport> Handle(ExpenseReportSaveCommand request) { _context.Update(request.ExpenseReport); _context.SaveChanges(); return(new SingleResult <ExpenseReport>(request.ExpenseReport)); }
public void ShouldPersistMultipleInstancesOfSameEmployee() { new DatabaseTester().Clean(); var employee = new Employee("1", "1", "1", "1"); var report = new ExpenseReport { Submitter = employee, Approver = employee, Title = "TestExpenseReport", Description = "This is an expense report test", Number = "123", Total = 100.25m }; IContainer container = DependencyRegistrarModule.EnsureDependenciesRegistered(); EfCoreContext context = container.GetInstance <EfCoreContext>(); context.Add(report); context.SaveChanges(); Employee approver = container.GetInstance <EfCoreContext>().Find <Employee>(employee.Id); Employee submitter = container.GetInstance <EfCoreContext>().Find <Employee>(employee.Id); report.Approver = approver; report.Submitter = submitter; EfCoreContext context2 = container.GetInstance <EfCoreContext>(); context2.Update(report); context2.SaveChanges(); }
public void UpdateAuthorWithLogging() { //SETUP string json; var showLog = false; var options = SqliteInMemory.CreateOptionsWithLogging <EfCoreContext>(log => { if (showLog) { _output.WriteLine(log.DecodeMessage()); } }); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var author = context.Books //#A .Where(p => p.Title == "Quantum Networking") //#A .Select(p => p.AuthorsLink.First().Author) //#A .Single(); //#A author.Name = "Future Person 2"; //#A json = JsonConvert.SerializeObject(author, //#A new JsonSerializerSettings() //#A { PreserveReferencesHandling = PreserveReferencesHandling.Objects //#A }); //#A } using (var context = new EfCoreContext(options)) { showLog = true; //ATTEMPT var author = JsonConvert .DeserializeObject <Author>(json); //#B context.Update(author); //#C context.SaveChanges(); //#D /********************************************************** #A This simulates an external system returning a modified Author entity class as a JSON string #B This simulates receiving a JSON string from an external system and decoding it into an Author class #C I use the Update command, which replaces all the row data for the given primary key, in this case AuthorId * *******************************************************/ //VERIFY var authorAgain = context.Books.Where(p => p.Title == "Quantum Networking") .Select(p => p.AuthorsLink.First().Author) .Single(); authorAgain.Name.ShouldEqual("Future Person 2"); context.Authors.Any(p => p.Name == "Future Person").ShouldBeFalse(); } }
public void UpdateAuthorWithLogging() { //SETUP var options = this.NewMethodUniqueDatabaseSeeded4Books(); string json; using (var context = new EfCoreContext(options)) //#A { //#A var author = context.Books //#A .Where(p => p.Title == "Quantum Networking") //#A .Select(p => p.AuthorsLink.First().Author) //#A .Single(); //#A author.Name = "Future Person 2"; //#A json = JsonConvert.SerializeObject(author); //#A } //#A using (var context = new EfCoreContext(options)) { var logIt = new LogDbContext(context); //REMOVE THIS //ATTEMPT var author = JsonConvert .DeserializeObject <Author>(json); //#B context.Update(author); //#C context.SaveChanges(); //#D /********************************************************** #A This simulates an external system returning a modified Author entity class as a JSON string #B This simulates receiving a JSON string from an external system and decoding it into an Author class #C I use the Update command, which replaces all the row data for the given primary key, in this case AuthorId * *******************************************************/ //VERIFY var authorAgain = context.Books.Where(p => p.Title == "Quantum Networking") .Select(p => p.AuthorsLink.First().Author) .Single(); authorAgain.Name.ShouldEqual("Future Person 2"); context.Authors.Any(p => p.Name == "Future Person").ShouldBeFalse(); foreach (var log in logIt.Logs) { _output.WriteLine(log); } } }
public void Update(IPlayer player) { using (var context = new EfCoreContext()) { try { var oldPlayer = context.Players.Where(p => p.Id == player.Id).SingleOrDefault(); oldPlayer = (Player)player; context.Update(oldPlayer); context.SaveChanges(); } catch (ArgumentException e) { throw new ArgumentException(e + " Player was not found"); } } }
public async Task<int> Update(UserTask task) { _context.Update(task); return await _context.SaveChangesAsync(); }