Home > Net >  Convert HierarchyId to string - unable to cast object of type 'System.Byte[]' to type 
Convert HierarchyId to string - unable to cast object of type 'System.Byte[]' to type 

Time:04-30

I'm using HierarchyId in my EFCore3.1, I have separated Read and Write in my application, In read, I have created a Model as below:

public class Category
{
    public string Id { get; set; }
    // removed for brevity
}

I should mention that in write it works fine. this is my model that I use in write:

public class Category
{
    public HierarchyId Id{ get; }
    // removed for brevity
}

I need when I run my application I get following error:

Unable to cast object of type 'System.Byte[]' to type 'System.String'.

Is there any way to convert HierarchyId to string?

Edit: I use entityframeworkcore.sqlserver.hierarchyid package.

CodePudding user response:

This is what I had to do:

      builder.Property( x => x.Id )
             .HasConversion(
                            v =>HierarchyId.Parse( v ),
                            v => v.ToString() ) ;
  • Related