-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[rc2] Fix null reference exception in migrations when string has invalid store type #36669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea042f9
Initial plan
Copilot 81b6abc
Fix null reference exception in migrations for invalid store types
Copilot fee1216
Changes before error encountered
Copilot cd9ba81
Remove empty test class and finalize implementation
Copilot 0ba8316
Update tests to verify exception message equals RelationalStrings.Uns…
Copilot 7ee5f54
Remove invalid column type test that doesn't represent affected scenario
Copilot 6b59217
Remove default value fallback
AndriySvyryd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
test/EFCore.SqlServer.Tests/Migrations/InvalidColumnTypeIntegrationTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.EntityFrameworkCore.TestUtilities; | ||
| using System.ComponentModel.DataAnnotations.Schema; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.SqlServer.Tests.Migrations; | ||
|
|
||
| /// <summary> | ||
| /// Integration test that simulates the user's original issue scenario more closely | ||
| /// </summary> | ||
| public class InvalidColumnTypeIntegrationTest | ||
|
AndriySvyryd marked this conversation as resolved.
Outdated
|
||
| { | ||
| [ConditionalFact] | ||
| public void Creating_migration_with_invalid_column_type_should_not_throw_null_reference() | ||
| { | ||
| var options = new DbContextOptionsBuilder<InvalidColumnTypeContext>() | ||
| .UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=TestInvalidColumnType;Trusted_Connection=true;MultipleActiveResultSets=true") | ||
| .Options; | ||
|
|
||
| using var context = new InvalidColumnTypeContext(options); | ||
|
|
||
| // This simulates the scenario where a user has invalid Column attributes | ||
| // Before the fix, this would throw a NullReferenceException | ||
| // After the fix, this should throw a more helpful InvalidOperationException | ||
| var ex = Assert.ThrowsAny<Exception>(() => | ||
| { | ||
| context.Database.EnsureDeleted(); | ||
| context.Database.EnsureCreated(); | ||
| }); | ||
|
|
||
| // We should NOT get a NullReferenceException | ||
| Assert.IsNotType<NullReferenceException>(ex); | ||
|
|
||
| // We should get some form of meaningful error instead | ||
| Assert.True(ex is InvalidOperationException || ex is ArgumentException || ex is NotSupportedException); | ||
| } | ||
|
|
||
| public class InvalidColumnTypeContext : DbContext | ||
| { | ||
| public InvalidColumnTypeContext(DbContextOptions options) : base(options) | ||
| { | ||
| } | ||
|
|
||
| public DbSet<Person> People { get; set; } | ||
| public DbSet<Contract> Contracts { get; set; } | ||
| } | ||
|
|
||
| public class Person | ||
| { | ||
| public int Id { get; set; } | ||
|
|
||
| [Column(TypeName = "decimal(18,2)")] // Invalid for string - this is what the user had | ||
| public string FirstName { get; set; } = string.Empty; | ||
|
|
||
| [Column(TypeName = "decimal(18,2)")] // Invalid for string - this is what the user had | ||
| public string LastName { get; set; } = string.Empty; | ||
|
|
||
| public int Age { get; set; } | ||
| public List<Contract> Contracts { get; set; } = new(); | ||
| } | ||
|
|
||
| public class Contract | ||
| { | ||
| public int Id { get; set; } | ||
| public string ContractLabel { get; set; } = string.Empty; | ||
| public DateTime DateSign { get; set; } = DateTime.Now; | ||
| public int? PersonId { get; set; } | ||
| public Person Person { get; set; } | ||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
test/EFCore.SqlServer.Tests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.EntityFrameworkCore.Migrations.Operations; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.SqlServer.Tests.Migrations; | ||
|
|
||
| public class SqlServerMigrationsSqlGeneratorNullReferenceTest : MigrationsSqlGeneratorTestBase | ||
| { | ||
| public SqlServerMigrationsSqlGeneratorNullReferenceTest() | ||
| : base(SqlServerTestHelpers.Instance) | ||
| { | ||
| } | ||
|
|
||
| protected override string GetGeometryCollectionStoreType() | ||
| => "geography"; | ||
|
|
||
| [ConditionalFact] | ||
| public void CreateTableOperation_with_invalid_string_column_store_type_should_not_throw_null_reference() | ||
|
AndriySvyryd marked this conversation as resolved.
Outdated
|
||
| { | ||
| // This should NOT throw a null reference exception when an invalid store type is specified for a string column | ||
| Generate( | ||
| new CreateTableOperation | ||
| { | ||
| Name = "People", | ||
| Columns = | ||
| { | ||
| new AddColumnOperation | ||
| { | ||
| Name = "FirstName", | ||
| Table = "People", | ||
| ClrType = typeof(string), | ||
| ColumnType = "decimal(18,2)", // Invalid store type for string | ||
| IsNullable = false | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Let's see what SQL is generated | ||
| Assert.NotEmpty(Sql); | ||
| // If we reach here, no null reference exception was thrown, which is correct | ||
| } | ||
|
|
||
| [ConditionalFact] | ||
| public void Reproduce_null_reference_with_invalid_column_type_during_migration_add() | ||
| { | ||
| // This test directly tests the fix by showing that | ||
| // when GetColumnType would return null, we now get a proper exception | ||
| // instead of a null reference exception | ||
|
|
||
| var operation = new AddColumnOperation | ||
| { | ||
| Name = "TestColumn", | ||
| Table = "TestTable", | ||
| ClrType = typeof(System.IO.FileStream), // This should definitely not have a mapping | ||
| ColumnType = null, // Force it to call GetColumnType | ||
| IsNullable = false | ||
| }; | ||
|
|
||
| // Let's see what SQL gets generated instead of what we expect | ||
| // This test validates that we don't get a NullReferenceException anymore | ||
| try | ||
| { | ||
| Generate(operation); | ||
| // If we get here without exception, the type mapping source found something unexpected | ||
| Assert.NotNull(Sql); | ||
| } | ||
| catch (NullReferenceException) | ||
| { | ||
| // This should NOT happen anymore with our fix | ||
| Assert.Fail("NullReferenceException should not be thrown - this indicates the fix didn't work"); | ||
| } | ||
| catch (InvalidOperationException ex) | ||
| { | ||
| // This is the expected behavior with our fix | ||
| Assert.Contains("type mapping", ex.Message); | ||
| } | ||
| // Any other exception is also acceptable as long as it's not NullReferenceException | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.