From 40d0d274c1dadd3a2250e3e5d86c0cff74013eb4 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Tue, 17 Dec 2024 11:42:10 -0800 Subject: [PATCH] Don't throw PendingModelChangesWarning when applying a specific migration Fixes #35342 --- .../Migrations/Internal/Migrator.cs | 9 +++++---- .../DataBindingTestBase.cs | 18 +++++++++--------- .../MigrationsInfrastructureSqlServerTest.cs | 3 +-- .../Query/JsonQueryJsonTypeSqlServerFixture.cs | 1 - 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/EFCore.Relational/Migrations/Internal/Migrator.cs b/src/EFCore.Relational/Migrations/Internal/Migrator.cs index ea758d2c634..4bfc0af691b 100644 --- a/src/EFCore.Relational/Migrations/Internal/Migrator.cs +++ b/src/EFCore.Relational/Migrations/Internal/Migrator.cs @@ -94,7 +94,7 @@ public Migrator( public virtual void Migrate(string? targetMigration) { var useTransaction = _connection.CurrentTransaction is null; - ValidateMigrations(useTransaction); + ValidateMigrations(useTransaction, targetMigration); using var transactionScope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled); @@ -219,7 +219,7 @@ public virtual async Task MigrateAsync( CancellationToken cancellationToken = default) { var useTransaction = _connection.CurrentTransaction is null; - ValidateMigrations(useTransaction); + ValidateMigrations(useTransaction, targetMigration); using var transactionScope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled); @@ -349,7 +349,7 @@ await _migrationCommandExecutor.ExecuteNonQueryAsync( } } - private void ValidateMigrations(bool useTransaction) + private void ValidateMigrations(bool useTransaction, string? targetMigration) { if (!useTransaction && _executionStrategy.RetriesOnFailure) @@ -365,7 +365,8 @@ private void ValidateMigrations(bool useTransaction) { _logger.ModelSnapshotNotFound(this, _migrationsAssembly); } - else if (RelationalResources.LogPendingModelChanges(_logger).WarningBehavior != WarningBehavior.Ignore + else if (targetMigration == null + && RelationalResources.LogPendingModelChanges(_logger).WarningBehavior != WarningBehavior.Ignore && HasPendingModelChanges()) { var modelSource = (ModelSource)_currentContext.Context.GetService(); diff --git a/test/EFCore.Specification.Tests/DataBindingTestBase.cs b/test/EFCore.Specification.Tests/DataBindingTestBase.cs index 118c6b500af..f8732999347 100644 --- a/test/EFCore.Specification.Tests/DataBindingTestBase.cs +++ b/test/EFCore.Specification.Tests/DataBindingTestBase.cs @@ -281,20 +281,20 @@ public virtual void Remove_detached_entity_from_LocalView(bool toObservableColle Assert.Equal(2, local.ToList().Count); Assert.True(local.Contains(driver1)); - Assert.True(localView.Contains(driver1)); + Assert.Contains(driver1, localView); Assert.True(local.Contains(driver2)); - Assert.True(localView.Contains(driver2)); + Assert.Contains(driver2, localView); Assert.False(local.Contains(driver3)); - Assert.False(localView.Contains(driver3)); + Assert.DoesNotContain(driver3, localView); context.Entry(driver3); Assert.True(local.Contains(driver1)); - Assert.True(localView.Contains(driver1)); + Assert.Contains(driver1, localView); Assert.True(local.Contains(driver2)); - Assert.True(localView.Contains(driver2)); + Assert.Contains(driver2, localView); Assert.False(local.Contains(driver3)); - Assert.False(localView.Contains(driver3)); + Assert.DoesNotContain(driver3, localView); local.Remove(driver3); @@ -302,11 +302,11 @@ public virtual void Remove_detached_entity_from_LocalView(bool toObservableColle Assert.Equal(2, local.ToList().Count); Assert.True(local.Contains(driver1)); - Assert.True(localView.Contains(driver1)); + Assert.Contains(driver1, localView); Assert.True(local.Contains(driver2)); - Assert.True(localView.Contains(driver2)); + Assert.Contains(driver2, localView); Assert.False(local.Contains(driver3)); - Assert.False(localView.Contains(driver3)); + Assert.DoesNotContain(driver3, localView); } [ConditionalTheory] diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs index f8116fc25bb..c9b682d2c62 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs @@ -807,8 +807,7 @@ public async Task Empty_Migration_Creates_Database() { using var context = new BloggingContext( Fixture.TestStore.AddProviderOptions( - new DbContextOptionsBuilder().EnableServiceProviderCaching(false)) - .ConfigureWarnings(e => e.Log(RelationalEventId.PendingModelChangesWarning)).Options); + new DbContextOptionsBuilder().EnableServiceProviderCaching(false)).Options); context.Database.EnsureDeleted(); GiveMeSomeTime(context); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/JsonQueryJsonTypeSqlServerFixture.cs b/test/EFCore.SqlServer.FunctionalTests/Query/JsonQueryJsonTypeSqlServerFixture.cs index 81642e9c14f..c125c5eccb6 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/JsonQueryJsonTypeSqlServerFixture.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/JsonQueryJsonTypeSqlServerFixture.cs @@ -26,7 +26,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con b => { b.OwnsOne(x => x.OwnedReferenceRoot).ToJson("json_reference_custom_naming").HasColumnType("json"); - ; b.OwnsMany(x => x.OwnedCollectionRoot).HasColumnType("json").ToJson("json_collection_custom_naming"); });