Skip to content

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

Closed

Description

@flindby

Old migration generated in EF Core 2.1.0 break in EF Core 8.0.5.

In the old migration (first in our project) there is a column defined as:

MyEntityType = table.Column<int>(nullable: false, defaultValue: "")

Which has been working until now when I upgraded to .NET8/EFCore8.
If I remove defaultValue: "" the migration works as expected.

Stacktrace:

[11:01:56]      Applying 20180809120006_Initial...
Object reference not set to an instance of an object.
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindCollectionMapping(RelationalTypeMappingInfo info, Type modelType, Type providerType, CoreTypeMapping elementMapping)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.<>c.<FindMappingWithConversion>b__8_0(ValueTuple`4 k, RelationalTypeMappingSource self)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMappingWithConversion(RelationalTypeMappingInfo mappingInfo, Type providerClrType, ValueConverter customConverter)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMappingWithConversion(RelationalTypeMappingInfo mappingInfo, IReadOnlyList`1 principals)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMapping(Type type, String storeTypeName, Boolean keyOrIndex, Nullable`1 unicode, Nullable`1 size, Nullable`1 rowVersion, Nullable`1 fixedLength, Nullable`1 precision, Nullable`1 scale)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.DefaultValue(Object defaultValue, String defaultValueSql, String columnType, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, ColumnOperation operation, IModel model, MigrationCommandListBuilder builder)

EF Core version: 8.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: (e.g. .NET 8.0)
Operating system: Windows 10

Activity

  1. modified the milestones: Backlog, 9.0.0 on Jul 12, 2024
  2. removed this from the 9.0.0 milestone on Aug 8, 2024
  3. 8 remaining items

  4. added this to the Backlog milestone on Aug 14, 2024
  5. removed their assignment
    on Aug 31, 2024
  6. added theissue type on Jun 15, 2025
  7. kirylbokach commented on Aug 18, 2025

    @kirylbokach

    I was having the same issue when trying to switch the discriminator column type from varchar(50) to smallint and forgetting to switch the property type from string to enum. Once switched the NRE goes away

    Generates NRE:

    builder.ToTable("signals");
    
    builder.UseTphMappingStrategy();
    
    const string discriminatorColumnName = "SignalClassDiscriminator";
    
    builder.HasDiscriminator<SignalClassDiscriminator>(discriminatorColumnName)
        .HasValue<ReceivedSignalDto>(SignalClassDiscriminator.Received)
        .HasValue<OriginatedSignalDto>(SignalClassDiscriminator.Originated);
    
    builder.Property<string>(discriminatorColumnName)
        .HasColumnType(KnownColumnTypes.IntEnum);

    Works:

     builder.ToTable("signals");
    
     builder.UseTphMappingStrategy();
    
    const string discriminatorColumnName = "SignalClassDiscriminator";
    
     builder.HasDiscriminator<SignalClassDiscriminator>(discriminatorColumnName
         .HasValue<ReceivedSignalDto>(SignalClassDiscriminator.Received)
         .HasValue<OriginatedSignalDto>(SignalClassDiscriminator.Originated);
    
     builder.Property<SignalClassDiscriminator>(discriminatorColumnName)
         .HasColumnType(KnownColumnTypes.IntEnum);

    The diff is in the line before the last one ( -> )

  8. self-assigned this
    on Aug 18, 2025
  9. modified the milestones: Backlog, 11.0.0 on Jun 8, 2026
  10. added a commit that references this issue on Jun 18, 2026
    5a63af4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions