简体   繁体   中英

.NET Core 3.1 and SQL Server hierarchyid - multiple exceptions

I work with SQL Server Db in my.Net Core 3.1 project and some stored procedures and views have hierarchyid types for parameters and data.

I use Microsoft.Data.SqlClient package. And when I try to read data with SqlDataReader I get the exception:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'. The system cannot find the file specified.

ok, I tried to use Microsoft.SqlServer.Types as it suggests but this package is not .NET Standard and it doesn't work.

Also, I found EntityFrameworkCore.SqlServer.HierarchyId but when I use it I get:

System.InvalidCastException: Unable to cast object of type 'Microsoft.SqlServer.Types.SqlHierarchyId' to type 'Microsoft.Data.SqlClient.Server.IBinarySerialize'.

So how on Earth can one use HierarchyId type in .NET Core 3.1?

I'm planning to host this solution on linux.

UPDATE

I do use Microsoft.Data.SqlClient 2.0 which is compatible with .NET Core. Also, I added then EntityFrameworkCore.SqlServer.HierarchyId , and I get this error:

System.InvalidCastException: Unable to cast object of type 'Microsoft.SqlServer.Types.SqlHierarchyId' to type 'Microsoft.Data.SqlClient.Server.IBinarySerialize'.

Here's the.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="EntityFrameworkCore.SqlServer.HierarchyId" Version="1.1.0" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Helpers\" />
  </ItemGroup>

</Project>

No luck so far.

UPDATE 2

This is the code where the exception is thrown:

using (SqlDataReader reader = await detailsCmd.ExecuteReaderAsync())
{
  while (reader.Read())
  {
    details.Add(new HierarchyDetails
    {
      Id = reader.GetInt32(0),
      groupPath = reader.GetValue(1).ToString(), // <==== EXCEPTION
      name = reader.GetString(2),
      optionalData = reader.IsDBNull(3) ? null : reader.GetString(3)
    });
  }
}

And the table has the only row:

id  groupPath   culture name    optionalData
24  0x58        en-US   testing 

Your error message suggests that you use something which uses nuget https://www.nuget.org/packages/Microsoft.SqlServer.Types/10.50.1600.1 which is .NET Framework dll, not .NET Core.

You mention that you use Microsoft.Data.SqlClient , please ensure that you use https://www.nuget.org/packages/Microsoft.Data.SqlClient/ which is compatible with .NET Core.

In case of other errors please check also Entity Framework Core hierarchyid

The solution at the moment to cast hierarchyid to NVARCHAR in all your queries:

... CAST(groupPath as NVARCHAR(4000)) as groupPath ...

and then use it as string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM