private void TestDetectChangesWithSaveChanges(bool autoDetectChanges) { using (var context = new F1Context()) { context.Database.Initialize(force: false); using (new TransactionScope()) { context.Configuration.AutoDetectChangesEnabled = autoDetectChanges; var mclaren = context.Teams.Find(Team.McLaren); var larryEntry = context.Entry(new Driver { Name = "Larry David" }); mclaren.Drivers.Add(larryEntry.Entity); Assert.Equal(autoDetectChanges ? EntityState.Added : EntityState.Detached, larryEntry.State); context.SaveChanges(); Assert.Equal(autoDetectChanges ? EntityState.Unchanged : EntityState.Detached, larryEntry.State); } } }
public void Collection_navigation_property_can_be_reloaded_even_if_marked_as_loaded() { using (var context = new F1Context()) { context.Configuration.LazyLoadingEnabled = false; var team = context.Teams.Find(Team.McLaren); var driversCollection = context.Entry(team).Collection(t => t.Drivers); // Load drivers for the first time driversCollection.Load(); Assert.True(driversCollection.IsLoaded); Assert.Equal(3, team.Drivers.Count); // Add a new driver to the database using (var innerContext = new F1Context()) { innerContext.Drivers.Add(new Driver { Name = "Larry David", TeamId = Team.McLaren }); innerContext.SaveChanges(); } // Now force load again Assert.True(driversCollection.IsLoaded); driversCollection.Load(); Assert.True(driversCollection.IsLoaded); Assert.Equal(4, team.Drivers.Count); } }