public void Reacts_to_principal_key_change_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] = 78; changeDetector.SidecarPropertyChanged(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))); }
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))); }
public void Reacts_to_principal_key_change_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] = 78; changeDetector.SidecarPropertyChanged(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))); }
public void Ignores_no_change_to_principal_key() { 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))); 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_non_principal_key_change_in_sidecar() { 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))); principalEntry.AddSidecar(new StoreGeneratedValuesFactory().Create(principalEntry))[property] = "Red"; changeDetector.SidecarPropertyChanged(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 ChangeTracker([NotNull] StateManager stateManager, [NotNull] ChangeDetector changeDetector) { Check.NotNull(stateManager, "stateManager"); Check.NotNull(changeDetector, "changeDetector"); _stateManager = stateManager; _changeDetector = changeDetector; }
public ChangeTracker( [NotNull] StateManager stateManager, [NotNull] ChangeDetector changeDetector, [NotNull] EntityEntryGraphIterator graphIterator, [NotNull] DbContextService <DbContext> context, [NotNull] EntityAttacherFactory attacherFactory) { Check.NotNull(stateManager, "stateManager"); Check.NotNull(changeDetector, "changeDetector"); Check.NotNull(graphIterator, "graphIterator"); Check.NotNull(context, "context"); Check.NotNull(attacherFactory, "attacherFactory"); StateManager = stateManager; _changeDetector = changeDetector; _graphIterator = graphIterator; _context = context; _attacherFactory = attacherFactory; }
public StateEntrySubscriber([NotNull] ChangeDetector changeDetector) { Check.NotNull(changeDetector, "changeDetector"); _changeDetector = changeDetector; }
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))); }
public void Ignores_no_change_to_principal_key() { 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))); 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_non_principal_key_change_in_sidecar() { 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))); principalEntry.AddSidecar(new StoreGeneratedValuesFactory().Create(principalEntry))[property] = "Red"; changeDetector.SidecarPropertyChanged(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))); }