LINQPad connection to EF Core 8.0 Assembly fails. Database provider does not support type HierarchyId

103 Views Asked by At

Connecting LINQPad using the built-in Entity Framework Core 8.0 Driver fails with InvalidOperationException: The 'HierarchyId' property 'domainObject.Property' could not be mapped because the database provider does not support this type. Consider converting the property value to a type supported by the database using a value converter. See https://aka.ms/efcore-docs-value-converters for more information. Alternately, exclude the property from the model using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."

Whilst I get this is an error returned to LINQPad, the assembly works fine and returns the HierarchyId object without issue within my Application.

Have upgraded to EF8 and LINQPadd8 so HierarchyId should be natively supported (as it appears to be in my Application).

1

There are 1 best solutions below

0
rich.rees.au On

For anyone else that lands here... I was passing the option to use HierarchyId from Program.cs.

// Add services to the container.
builder.Services.AddDbContext<TempDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("MyDB"), x => x.UseHierarchyId()), ServiceLifetime.Scoped);

Obviously this wasn't being included in the assembly I was trying to use in Linqpad.

What I should have been doing was adding that option via the OnConfiguring method of the DbContect. This resolved my issue.

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
    options.UseSqlServer(conf =>
    {
        conf.UseHierarchyId();
    });
}

So whilst the assembly worked in my Application, it was only because the UseHierarchyId option was being passed in at construction.