public async Task TwoContexts() { using (var context = new WorkSpeedDbContext()) { context.Database.Migrate(); } using (var context1 = new WorkSpeedDbContext()) { using (var context2 = new WorkSpeedDbContext()) { // create var employee = new Employee { Id = "AR00001", Name = "Test Employee", IsActive = true }; context1.Add(employee); context1.SaveChanges(); foreach (var entityEntry in context2.ChangeTracker.Entries <Employee>()) { entityEntry.Reload(); } var empl = context2.Employees.ToArray(); Assert.That(empl[0].Name, Is.EqualTo(employee.Name)); Assert.That(empl.Length, Is.EqualTo(1)); // update var newName = "John Doe"; employee = context1.Employees.ToArray()[0]; employee.Name = newName; context1.SaveChanges(); employee = new Employee { Id = "AR00002", Name = "Test Employee", IsActive = true }; context1.Add(employee); context1.SaveChanges(); var employees = context1.Employees.ToArray(); Assert.That(employees.Length, Is.EqualTo(2)); foreach (var entityEntry in context2.ChangeTracker.Entries <Employee>()) { await entityEntry.ReloadAsync(); } empl = context2.Employees.ToArray(); Assert.That(empl[0].Name, Is.EqualTo(newName)); Assert.That(empl.Length, Is.EqualTo(2)); } } }
private ReportService GetReportService(bool fillEmployees = true) { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); var options = new DbContextOptionsBuilder <WorkSpeedDbContext>().UseSqlite(_connection).Options; using (var dbContext = new WorkSpeedDbContext(options)) { dbContext.Database.EnsureCreated(); if (fillEmployees) { var shifts = dbContext.Shifts.ToArray(); var appointments = dbContext.Appointments.ToArray(); var positions = dbContext.Positions.ToArray(); dbContext.Employees.AddRange(new Employee[] { new Employee { Id = "AR00001", Name = "Mnager", IsActive = true, Shift = shifts[0], Appointment = appointments[7], Position = positions[0] }, new Employee { Id = "AR00002", Name = "Manager Assistant 1", IsActive = true, Shift = shifts[0], Appointment = appointments[6], Position = positions[1] }, new Employee { Id = "AR00003", Name = "Manager Assistant 2", IsActive = true, Shift = shifts[0], Appointment = appointments[5], Position = positions[2] }, new Employee { Id = "AR00004", Name = "Principal Warehouseman 1", IsActive = true, Shift = shifts[0], Appointment = appointments[4], Position = positions[1] }, new Employee { Id = "AR00005", Name = "Principal Warehouseman 2", IsActive = true, Shift = shifts[0], Appointment = appointments[4], Position = positions[2] }, new Employee { Id = "AR00006", Name = "Principal Warehouseman 3", IsActive = true, Shift = shifts[1], Appointment = appointments[4], Position = positions[1] }, }); dbContext.SaveChanges(); } }; var stub = new Mock <IProductivityBuilder>(); var reportService = new ReportService(new WorkSpeedDbContext(options), stub.Object); return(reportService); }