Skip to content

Fix NullReferenceException for mistyped default values on non-string columns in migrations#38399

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-old-migration-ef-core-8
Open

Fix NullReferenceException for mistyped default values on non-string columns in migrations#38399
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-old-migration-ef-core-8

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Migrations authored in older EF Core versions can carry a default value whose CLR type doesn't match the column's store type — e.g. table.Column<int>(nullable: false, defaultValue: ""). This worked through EF Core 7 but throws an NullReferenceException when applied under EF Core 8+.

new AddColumnOperation { ClrType = typeof(int), ColumnType = "int", DefaultValue = "", IsNullable = false }
// EF8: NullReferenceException in RelationalTypeMappingSource.FindCollectionMapping

Root cause

MigrationsSqlGenerator.DefaultValue calls FindMapping(typeof(string), "int"). The scalar lookup misses, so resolution falls into the collection-mapping path, where TypeMappingSourceBase.TryFindJsonCollectionMapping treats string as a primitive collection of char (string : IEnumerable<char>). The follow-up mapping lookup returns null, and null!.WithComposedConverter(...) dereferences it.

Changes

  • TypeMappingSourceBase.TryFindJsonCollectionMapping: guard against modelClrType == typeof(string). string always has dedicated scalar mappings and is never a JSON/primitive collection, so it should not enter this path. The mistyped default now resolves to null and DefaultValue falls back to GetMappingForValue, yielding valid SQL (... DEFAULT N''), matching legacy behavior.
  • SqlServerMigrationsSqlGeneratorTest: added AddColumnOperation_mistyped_default_legacy covering the int-column / string-default case.
 if ((providerClrType == null || providerClrType == typeof(string))
+    && modelClrType != typeof(string)
     && modelClrType.TryGetElementType(typeof(IEnumerable<>)) is { } elementType
     && elementType != modelClrType
     && !modelClrType.GetGenericTypeImplementations(typeof(IDictionary<,>)).Any())

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix old migration break in EF Core 8 due to invalid default value Fix NullReferenceException for mistyped default values on non-string columns in migrations Jun 10, 2026
Copilot AI requested a review from AndriySvyryd June 10, 2026 18:44
@AndriySvyryd AndriySvyryd requested a review from Copilot June 10, 2026 19:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression where applying older migrations with a mistyped DefaultValue (e.g. int column with defaultValue: "") can throw a NullReferenceException in EF Core 8+ during type mapping resolution. The change prevents string from being misclassified as a primitive collection (IEnumerable<char>) in the JSON collection-mapping path, restoring the prior “fallback” behavior and producing valid SQL.

Changes:

  • Update TypeMappingSourceBase.TryFindJsonCollectionMapping to explicitly exclude modelClrType == typeof(string) from the JSON/primitive-collection mapping flow.
  • Add a SQL Server migrations SQL generator test covering AddColumnOperation with an int column and string default value to ensure no exception and stable SQL generation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/EFCore/Storage/TypeMappingSourceBase.cs Prevents string from entering JSON collection mapping, avoiding the NRE and allowing downstream fallback mapping for mistyped defaults.
test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs Adds a regression test asserting the generated SQL for the mistyped-default scenario.

@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 10, 2026 21:09
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 10, 2026 21:09
@AndriySvyryd AndriySvyryd requested a review from cincuranet June 10, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Old migration break in EF Core 8 that worked in EF Core 7 and previous

3 participants