Exemple #1
0
        public void AfterSaveChanges_Entity_RelatedEntity_Added()
        {
            string result = string.Empty;
            string resultFormat = "AfterSaveChanges_Entity_RelatedEntity_Added_{0}_{1}";

            TrackingContext.Current.Subscribe<EFTrackingContext>(TrackingEvent.AfterSaveChanges, (s) =>
                {
                    s.For<Project>()
                        .Where(x => x.Departments, RelatedEntityTrackingTypes.Added)
                        .Perform((content, item) =>
                            {
                                result = string.Format(resultFormat, item.Entity.Name, item.RelatedEntity.Name);
                            });
                });

            using(var db = new EFTrackingContext())
            {
                var project = db.Projects.SingleOrDefault(x=>x.Name == UT_ProjectName);
                if (project == null)
                {
                    project = new Project { Name = UT_ProjectName };
                    db.Projects.Add(project);
                    db.SaveChanges();
                }

                var department = new Department{ Name = UT_DepartmentName};
                project.Departments.Add(department);
                db.SaveChanges();

                Assert.AreEqual(string.Format(resultFormat, project.Name, department.Name), result, "AfterSaveChanges_Entity_RelatedEntity_Added - Failed");
            }
        }
Exemple #2
0
        public void AfterSaveChanges_Entity_RelatedEntity_Deleted()
        {
            bool deleted = false;

            TrackingContext.Current.Subscribe<EFTrackingContext>(TrackingEvent.AfterSaveChanges, (s) =>
            {
                s.For<Project>()
                    .Where(x => x.Departments, RelatedEntityTrackingTypes.Deleted)
                    .Perform((content, item) =>
                    {
                        deleted = true;
                    });
            });

            using (var db = new EFTrackingContext())
            {
                var project = db.Projects.SingleOrDefault(x => x.Name == UT_ProjectName);
                if (project == null)
                {
                    project = new Project { Name = UT_ProjectName };
                    db.Projects.Add(project);
                    db.SaveChanges();
                }

                var department = project.Departments.SingleOrDefault(x => x.Name == UT_DepartmentName);
                if (department == null)
                {
                    department = new Department { Name = UT_DepartmentName };
                    project.Departments.Add(department);
                    db.SaveChanges();
                }

                project.Departments.Remove(department);
                db.SaveChanges();

                Assert.IsTrue(deleted, "AfterSaveChanges_Entity_RelatedEntity_Deleted - Failed");
            }
        }