Repository navigation
Description
Bug description
After migrating from EF 9 where some entities had an Embedding using a float[] type, migrations stopped working, with the following error:
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.SqlServer.Storage.Internal.SqlServerTypeMappingSource.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(IProperty property)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Property.<>c.<get_TypeMapping>b__96_0(IProperty property)
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Property.get_TypeMapping()
at Microsoft.EntityFrameworkCore.Metadata.Internal.Property.Microsoft.EntityFrameworkCore.Metadata.IReadOnlyProperty.FindTypeMapping()
at Microsoft.EntityFrameworkCore.RelationalPropertyExtensions.FindRelationalTypeMapping(IReadOnlyProperty property)
at Microsoft.EntityFrameworkCore.RelationalPropertyExtensions.GetColumnType(IReadOnlyProperty property)
at Microsoft.EntityFrameworkCore.RelationalPropertyExtensions.GetColumnType(IProperty property)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.CreateDefaultColumnMapping(ITypeBase typeBase, ITypeBase mappedType, TableBase defaultTable, TableMappingBase`1 tableMapping, Boolean isTph, Boolean isTpc, IRelationalTypeMappingSource relationalTypeMappingSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.AddDefaultMappings(RelationalModel databaseModel, IEntityType entityType, IRelationalTypeMappingSource relationalTypeMappingSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.Create(IModel model, IRelationalAnnotationProvider relationalAnnotationProvider, IRelationalTypeMappingSource relationalTypeMappingSource, Boolean designTime)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelRuntimeInitializer.<>c__DisplayClass4_0.<InitializeModel>b__0()
at Microsoft.EntityFrameworkCore.RelationalModelExtensions.<>c.<GetRelationalModel>b__4_0(Func`1 f)
at Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.<>c__30`2.<GetOrAddRuntimeAnnotationValue>b__30_0(String n, ValueTuple`3 t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
at Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.GetOrAddRuntimeAnnotationValue[TValue,TArg](String name, Func`2 valueFactory, TArg factoryArgument)
at Microsoft.EntityFrameworkCore.RelationalModelExtensions.GetRelationalModel(IModel model)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
My entity was already configured using the fluent API:
builder.Property(e => e.Embedding)
.HasColumnType("vector(1536)");The property is now using the SqlVector type:
public SqlVector<float>? Embedding { get; set; }Am I missing something?
Your code
Code is the same as it was in .NET 9 version, except for the new type that has changed to SqlVector<float>Stack traces
Verbose output
EF Core version
10.0.7
Database provider
No response
Target framework
.NET 10
Operating system
No response
IDE
VS 2026 18.3.3
Reacted by Copilot
Activity
arielmoraes commentedon Apr 25, 2026
I think I found the problem, I set a breakpoint in the
RelationalTypeMappingSource.FindCollectionMappingmethod and found that the problem was linked to the SqlVector type. The modelType parameter was being passed with the Type String, so EF couldn't find a mapping between String and SqlVector.With that finding I went to the ModelSnapshot and the Embedding property was defined as:
But the new SqlVector type generates the code as:
So if someone was using the https://github.com/efcore/EFCore.SqlServer.VectorSearch which @roji built, there will be some problems when trying to migrate. I don't know if there is a better approach, but the workaround I found was to edit the ModelSnapshot manually.
roji commentedon Apr 25, 2026
AI Triage
The below is an AI-generated analysis and may contain inaccuracies.
Summary
Confirmed bug. A
NullReferenceExceptionoccurs inRelationalTypeMappingSource.FindCollectionMappingwhen adding a new migration after upgrading from EF 9 (with the community EFCore.SqlServer.VectorSearch package) to EF 10 (with built-inSqlVector<float>support).The old model snapshot contains
PrimitiveCollection<string>("Embedding").HasColumnType("vector(1536)")(generated by the community package), while the current model usesSqlVector<float>?. Whendotnet ef migrations addruns, the snapshot model is processed without validation (by design,SnapshotModelProcessor.ProcesspassesvalidationLogger: null), so theValidatePrimitiveCollectioncheck doesn't run. When the relational model is then created for the snapshot,FindCollectionMappingis called for thestringprimitive collection with store typevector(1536), and the NRE occurs.Root Cause
In
RelationalTypeMappingSource.FindCollectionMapping:TryFindJsonCollectionMappingreturnstrueforstring(sincestringimplementsIEnumerable<char>)FindMapping(info.WithConverter(...))is called to find a mapping forstringwith store typevector(1536), but no such mapping exists, so it returnsnullnullreturn value is null-forgivingly dereferenced with!, and.WithComposedConverter()is called onnull, causing the NREThe same pattern exists in the SQL Server override (
SqlServerTypeMappingSource.FindCollectionMapping), which has the same null-dereference risk at line 413.Possibly Related Issues
FindCollectionMappingfor old migrations with incompatible store types (open since EF 8)stringas primitive collection (different error, fixed)Minimal repro
1. Create a new console project and add packages:
2. Program.cs:
3. Create
Migrations/TestContextModelSnapshot.cs(simulates old snapshot from community package):4. Create
Migrations/20250101000000_Initial.csand its Designer file (a matching initial migration).5. Run:
Result:
System.NullReferenceException: Object reference not set to an instance of an object.atFindCollectionMapping.Workaround
Manually edit the ModelSnapshot file to replace:
with:
(adding
using Microsoft.Data.SqlTypes;at the top).roji commentedon Apr 26, 2026
@AndriySvyryd this seems to be a dup (or very related?) to #33734, but here around the transition from the older SQL Server JSON support (which used string) to the newer (which uses
SqlVector<float>). It's probably OK to require users to edit their model snapshots (though if there's a better fix that would obviously be better), but we should at least fix the exception...Let me know what you think.
arielmoraes commentedon May 2, 2026
@roji, the problem seems to be more complex than just editing the ModelSnapshot for generating new migrations. There are some issues when applying migrations.
The .designer files for each migration also have references to
b.PrimitiveCollection<string>("Embedding"), so those must also be edited, otherwise environments that don't have the migration applied yet will have the same problem.Prevent `FindCollectionMapping` NRE on legacy string/vector snapshot …