예제 #1
0
        public void Detects_principal_key_change()
        {
            var model = BuildModel();
            var configuration = TestHelpers.CreateContextConfiguration(model);
            var stateManager = configuration.Services.StateManager;

            var entityType = model.GetEntityType(typeof(Category));
            var keyProperty = entityType.GetProperty("PrincipalId");

            var category = new Category { Id = -1, PrincipalId = 77 };
            var principalEntry = stateManager.StartTracking(stateManager.GetOrCreateEntry(category));
            principalEntry.RelationshipsSnapshot[keyProperty] = 77;
            principalEntry.EntityState = EntityState.Added;

            var notifierMock = new Mock<StateEntryNotifier>();
            var changeDetector = new ChangeDetector(configuration, notifierMock.Object);

            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey<int>(entityType, -1)));

            category.PrincipalId = 78;
            changeDetector.PropertyChanged(principalEntry, keyProperty);

            notifierMock.Verify(m => m.PrincipalKeyPropertyChanged(principalEntry, keyProperty, 77, 78));

            Assert.Equal(78, principalEntry.RelationshipsSnapshot[keyProperty]);
            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey<int>(entityType, -1)));
        }
예제 #2
0
        public void Detects_primary_key_change()
        {
            var model         = BuildModel();
            var configuration = TestHelpers.CreateContextConfiguration(model);
            var stateManager  = configuration.Services.StateManager;

            var entityType  = model.GetEntityType(typeof(Category));
            var keyProperty = entityType.GetProperty("Id");

            var category = new Category {
                Id = -1
            };
            var principalEntry = stateManager.StartTracking(stateManager.GetOrCreateEntry(category));

            principalEntry.RelationshipsSnapshot[keyProperty] = -1;
            principalEntry.EntityState = EntityState.Added;

            var notifierMock   = new Mock <StateEntryNotifier>();
            var changeDetector = new ChangeDetector(configuration, notifierMock.Object);

            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey <int>(entityType, -1)));

            category.Id = 1;
            changeDetector.PropertyChanged(principalEntry, keyProperty);

            notifierMock.Verify(m => m.PrincipalKeyPropertyChanged(principalEntry, keyProperty, -1, 1));

            Assert.Equal(1, principalEntry.RelationshipsSnapshot[keyProperty]);
            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey <int>(entityType, 1)));
        }
예제 #3
0
        public void Ignores_no_change_to_principal_key_in_sidecar()
        {
            var model         = BuildModel();
            var configuration = TestHelpers.CreateContextConfiguration(model);
            var stateManager  = configuration.Services.StateManager;

            var entityType  = model.GetEntityType(typeof(Category));
            var keyProperty = entityType.GetProperty("PrincipalId");

            var category = new Category {
                Id = -1, PrincipalId = 77
            };
            var principalEntry = stateManager.StartTracking(stateManager.GetOrCreateEntry(category));

            principalEntry.RelationshipsSnapshot[keyProperty] = 77;
            principalEntry.EntityState = EntityState.Added;

            var notifierMock   = new Mock <StateEntryNotifier>();
            var changeDetector = new ChangeDetector(configuration, notifierMock.Object);

            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey <int>(entityType, -1)));

            principalEntry.AddSidecar(new StoreGeneratedValuesFactory().Create(principalEntry))[keyProperty] = 77;
            changeDetector.PropertyChanged(principalEntry, keyProperty);

            notifierMock.Verify(m => m.PrincipalKeyPropertyChanged(
                                    It.IsAny <StateEntry>(), It.IsAny <IProperty>(), It.IsAny <object>(), It.IsAny <object>()), Times.Never);

            Assert.Equal(77, principalEntry.RelationshipsSnapshot[keyProperty]);
            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey <int>(entityType, -1)));
        }
예제 #4
0
        public void Ignores_non_principal_key_change()
        {
            var model         = BuildModel();
            var configuration = TestHelpers.CreateContextConfiguration(model);
            var stateManager  = configuration.Services.StateManager;

            var entityType = model.GetEntityType(typeof(Category));
            var property   = entityType.GetProperty("Name");

            var category = new Category {
                Id = -1, Name = "Blue"
            };
            var principalEntry = stateManager.StartTracking(stateManager.GetOrCreateEntry(category));

            principalEntry.RelationshipsSnapshot[property] = "Blue";
            principalEntry.EntityState = EntityState.Added;

            var notifierMock   = new Mock <StateEntryNotifier>();
            var changeDetector = new ChangeDetector(configuration, notifierMock.Object);

            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey <int>(entityType, -1)));

            category.Name = "Red";
            changeDetector.PropertyChanged(principalEntry, property);

            notifierMock.Verify(m => m.PrincipalKeyPropertyChanged(
                                    It.IsAny <StateEntry>(), It.IsAny <IProperty>(), It.IsAny <object>(), It.IsAny <object>()), Times.Never);

            Assert.Equal("Blue", principalEntry.RelationshipsSnapshot[property]);
            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey <int>(entityType, -1)));
        }
        public virtual StateEntry SnapshotAndSubscribe([NotNull] StateEntry entry)
        {
            var entityType = entry.EntityType;

            if (!entityType.UseLazyOriginalValues)
            {
                entry.OriginalValues.TakeSnapshot();
                entry.RelationshipsSnapshot.TakeSnapshot();
            }

            var changing = entry.Entity as INotifyPropertyChanging;

            if (changing != null)
            {
                changing.PropertyChanging += (s, e) =>
                {
                    var property = TryGetPropertyBase(entityType, e.PropertyName);
                    if (property != null)
                    {
                        _changeDetector.PropertyChanging(entry, property);
                    }
                };
            }

            var changed = entry.Entity as INotifyPropertyChanged;

            if (changed != null)
            {
                changed.PropertyChanged += (s, e) =>
                {
                    var property = TryGetPropertyBase(entityType, e.PropertyName);
                    if (property != null)
                    {
                        _changeDetector.PropertyChanged(entry, property);
                    }
                };
            }

            return(entry);
        }
예제 #6
0
        public void Ignores_no_change_to_principal_key_in_sidecar()
        {
            var model = BuildModel();
            var configuration = TestHelpers.CreateContextConfiguration(model);
            var stateManager = configuration.Services.StateManager;

            var entityType = model.GetEntityType(typeof(Category));
            var keyProperty = entityType.GetProperty("PrincipalId");

            var category = new Category { Id = -1, PrincipalId = 77 };
            var principalEntry = stateManager.StartTracking(stateManager.GetOrCreateEntry(category));
            principalEntry.RelationshipsSnapshot[keyProperty] = 77;
            principalEntry.EntityState = EntityState.Added;

            var notifierMock = new Mock<StateEntryNotifier>();
            var changeDetector = new ChangeDetector(configuration, notifierMock.Object);

            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey<int>(entityType, -1)));

            principalEntry.AddSidecar(new StoreGeneratedValuesFactory().Create(principalEntry))[keyProperty] = 77;
            changeDetector.PropertyChanged(principalEntry, keyProperty);

            notifierMock.Verify(m => m.PrincipalKeyPropertyChanged(
                It.IsAny<StateEntry>(), It.IsAny<IProperty>(), It.IsAny<object>(), It.IsAny<object>()), Times.Never);

            Assert.Equal(77, principalEntry.RelationshipsSnapshot[keyProperty]);
            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey<int>(entityType, -1)));
        }
예제 #7
0
        public void Ignores_non_principal_key_change()
        {
            var model = BuildModel();
            var configuration = TestHelpers.CreateContextConfiguration(model);
            var stateManager = configuration.Services.StateManager;

            var entityType = model.GetEntityType(typeof(Category));
            var property = entityType.GetProperty("Name");

            var category = new Category { Id = -1, Name = "Blue" };
            var principalEntry = stateManager.StartTracking(stateManager.GetOrCreateEntry(category));
            principalEntry.RelationshipsSnapshot[property] = "Blue";
            principalEntry.EntityState = EntityState.Added;

            var notifierMock = new Mock<StateEntryNotifier>();
            var changeDetector = new ChangeDetector(configuration, notifierMock.Object);

            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey<int>(entityType, -1)));

            category.Name = "Red";
            changeDetector.PropertyChanged(principalEntry, property);

            notifierMock.Verify(m => m.PrincipalKeyPropertyChanged(
                It.IsAny<StateEntry>(), It.IsAny<IProperty>(), It.IsAny<object>(), It.IsAny<object>()), Times.Never);

            Assert.Equal("Blue", principalEntry.RelationshipsSnapshot[property]);
            Assert.Same(principalEntry, stateManager.TryGetEntry(new SimpleEntityKey<int>(entityType, -1)));
        }