Ejemplo n.º 1
0
        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 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 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);
        }