I'm struggling to get migrations to run in EF Core with a HierarchyId column.
I've created a test project that is based on the code from @aljones and @dotMorten but I've replaced the dependency on System.Data.SqlClient with Microsoft.Data.SqlClient.
The QueryTests all pass, but when I attempt to generate migrations from the EFCore.SqlServer.HierarchyId.Test folder it fails as below:
dotnet ef migrations add InitialCreate
Build started...
Build succeeded.
System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'System.Data.SqlTypes.SqlBytes'. Configure your services to use one that can.
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(Object[,] values)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(InsertDataOperation operation, IndentedStringBuilder builder)
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(String builderName, IReadOnlyList`1 operations, IndentedStringBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration(String migrationNamespace, String migrationName, IReadOnlyList`1 upOperations, IReadOnlyList`1 downOperations)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
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)
The current CSharpHelper cannot scaffold literals of type 'System.Data.SqlTypes.SqlBytes'. Configure your services to use one that can.
Is there something that needs adding so that EF Core will generate the migration?
EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1.101
Operating system: Windows
IDE: Visual Studio 2019 16.4
@bricelam Could this be because of the change to support bytes directly on UDT parameters? (You know, the thing that went into SqlClient as a community PR, and which we then responded to.)
If so, supporting the scaffolding of SqlBytes should fix it, right?
You need to override GenerateCodeLiteral in your SqlServerHierarchyIdTypeMapping.
For example, for Geometry, we generate an expression like (Point)new WKTReader().Read("POINT(0 0)"):
Most helpful comment
You need to override GenerateCodeLiteral in your SqlServerHierarchyIdTypeMapping.
For example, for Geometry, we generate an expression like
(Point)new WKTReader().Read("POINT(0 0)"):https://github.com/dotnet/efcore/blob/444c29fa550c1a566a2362278fff0f6744bfb964/src/EFCore.Relational/Storage/RelationalGeometryTypeMapping.cs#L126-L132