public void DeleteAggregateChildFromAggregateRootAndDatabase() { DatabaseContext ctx = new DatabaseContext(); Employee supervisorEmployee = ctx.EmployeeRepository.Find(1); Employee subordinateEmployee = supervisorEmployee.Subordinates.Find(e => e.Id == 3); /*********************************** * If the foreign key was non-nullable, the following error will occur when the following line of code is executed: * The relationship could not be changed because one or more of the foreign-key properties is non-nullable. * When a change is made to a relationship, the related foreign-key property is set to a null value. * If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. * * NEED TO CHECK: For a composite child key, the corresponding row will be physically deleted!? *************************************/ //supervisorEmployee.Subordinates.Remove(subordinateEmployee); ctx.Entry(subordinateEmployee).State = EntityState.Deleted; ctx.SaveChanges(); int actualCount = (from e in ctx.EmployeeRepository select e).ToList().Count; Assert.AreEqual(13, actualCount); Assert.AreEqual(9, supervisorEmployee.Subordinates.Count); }
public void InsertAggregateChildToAggregateRoot(){ DatabaseContext ctx = new DatabaseContext(); Employee supervisorEmployee = ctx.EmployeeRepository.Find(1); Employee subordinateEmployee = TestDataHelper.CreateEmployeeWithValidData(); supervisorEmployee.Subordinates.Add(subordinateEmployee); //marking to add to database ctx.SaveChanges(); int actualCount = (from e in ctx.EmployeeRepository select e).ToList().Count; Assert.AreEqual(15, actualCount); Assert.AreEqual(11, supervisorEmployee.Subordinates.Count); }
public void InsertAggregateChildToNewAggregateRootObject() { Employee supervisorEmployee = TestDataHelper.CreateEmployeeWithValidData(); Employee subordinateEmployee = TestDataHelper.CreateEmployeeWithValidData(); supervisorEmployee.Subordinates = new List<Employee>(); supervisorEmployee.Subordinates.Add(subordinateEmployee); //marking to add to database DatabaseContext ctx = new DatabaseContext(); ctx.Entry(supervisorEmployee).State = EntityState.Added; ctx.SaveChanges(); int actualCount = (from e in ctx.EmployeeRepository select e).ToList().Count; Assert.AreEqual(16, actualCount); supervisorEmployee = ctx.EmployeeRepository.Find(15); Assert.AreEqual(1, supervisorEmployee.Subordinates.Count); }
public void UpdatedAggregateChildFromAggregateRoot() { TestInitialize(); DatabaseContext ctx = new DatabaseContext(); Employee supervisorEmployee = ctx.EmployeeRepository.Find(1); Employee subordinateEmployee = supervisorEmployee.Subordinates.Find(e => e.Id == 3); subordinateEmployee.FirstName = "Md."; ctx.SaveChanges(); Employee sub = ctx.EmployeeRepository.Find(3); Assert.Equal("Md.", sub.FirstName); }
public void DeleteAggregateRootWithAllAggregateChild() { TestInitialize(); DatabaseContext ctx = new DatabaseContext(); Employee supervisorEmployee = ctx.EmployeeRepository.Find(1); List<Employee> subordinates = ( from employee in ctx.EmployeeRepository where employee.ReportsTo == 1 select employee ).ToList(); foreach (Employee sub in subordinates) ctx.EmployeeRepository.Remove(sub); ctx.Entry(supervisorEmployee).State = EntityState.Deleted; ctx.SaveChanges(); int actualCount = (from e in ctx.EmployeeRepository select e).ToList().Count; Assert.Equal(3, actualCount); }
public void UpdateAggregateChildToNullAggregateRoot() { TestInitialize(); DatabaseContext ctx = new DatabaseContext(); Employee supervisorEmployee = ctx.EmployeeRepository.Find(1); Employee subordinateEmployee = supervisorEmployee.Subordinates.Find(e => e.Id == 3); supervisorEmployee.Subordinates.Remove(subordinateEmployee); //updates child's parent reference (foreign) to null ctx.SaveChanges(); int actualCount = (from e in ctx.EmployeeRepository select e).ToList().Count; Assert.Equal(14, actualCount); Assert.Equal(9, supervisorEmployee.Subordinates.Count); }
public void UpdatedAggregateChildToAnotherAggregateRoot() { TestInitialize(); DatabaseContext ctx = new DatabaseContext(); Employee supervisorEmployee = ctx.EmployeeRepository.Find(1); Employee supervisorEmployee2 = ctx.EmployeeRepository.Find(2); Employee subordinateEmployee = supervisorEmployee.Subordinates.Find(e => e.Id == 3); supervisorEmployee2.Subordinates.Add(subordinateEmployee); ctx.SaveChanges(); int actualCount = (from e in ctx.EmployeeRepository select e).ToList().Count; Assert.Equal(14, actualCount); Assert.Equal(9, supervisorEmployee.Subordinates.Count); Assert.Equal(3, supervisorEmployee2.Subordinates.Count); }