Skip to content

ValueConverter does not work well with HasData #21558

@smitpatel

Description

@smitpatel

Decided to take a trip down the rabbit hole.

https://github.com/efcore/EFCore.SqlServer.HierarchyId/
Adds a type HierarchyId to typeMappingSource and utilizes a value converter to convert it to provider clr type of SqlBytes.
When populating HasData and generating Snapshot,

  • We use the provider clr type from the value converter rather than using HierarchyId, this in turn creates properties of SqlBytes in the model. (we add using statement so this part works ok).
  • Getting seed data converts HierarchyId values to SqlBytes values and now we try to print SqlBytes values in the snapshot.
  • The printing part is not aware of the actual type mapping from property which had value converter. So it tries to find type mapping for SqlBytes which of course does not exist hence we throw exception.

Same also exists for our own Geometry types in NTS packages.
The way both code had "work-around" is, have a ValueConverter but not let EF know about it so EF Core will have HierarchyId everywhere, this eventually invokes GenerateCodeLiteral on HierarchyIdTypeMapping which can print out literal. And whenever methods on HierarchyIdTypeMapping are invoked use the value converter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions