Esempio n. 1
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)));
        }
Esempio n. 2
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)));
        }
Esempio n. 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)));
        }
        public void Ignores_no_change_to_principal_key()
        {
            var notifierMock    = new Mock <StateEntryNotifier>();
            var model           = BuildModel();
            var contextServices = CreateContextServices(notifierMock.Object, model);
            var stateManager    = contextServices.GetRequiredService <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 changeDetector = new ChangeDetector(new DbContextService <IModel>(model));

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

            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)));
        }