简体   繁体   English

.NET Core 3.1 和 SQL 服务器hierarchyid - 多个异常

[英].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.我在 my.Net Core 3.1 项目中使用 SQL Server Db,并且一些存储过程和视图具有参数和数据的层次结构类型。

I use Microsoft.Data.SqlClient package.我使用Microsoft.Data.SqlClient package。 And when I try to read data with SqlDataReader I get the exception:当我尝试使用SqlDataReader读取数据时,出现异常:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'. System.IO.FileNotFoundException:无法加载文件或程序集“Microsoft.SqlServer.Types,版本=10.0.0.0,文化=中性,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.好的,我尝试按照它的建议使用Microsoft.SqlServer.Types ,但是这个 package 不是 .NET 标准,它不起作用。

Also, I found EntityFrameworkCore.SqlServer.HierarchyId but when I use it I get:另外,我找到了EntityFrameworkCore.SqlServer.HierarchyId但是当我使用它时,我得到:

System.InvalidCastException: Unable to cast object of type 'Microsoft.SqlServer.Types.SqlHierarchyId' to type 'Microsoft.Data.SqlClient.Server.IBinarySerialize'. System.InvalidCastException:无法将类型为“Microsoft.SqlServer.Types.SqlHierarchyId”的 object 转换为类型“Microsoft.Data.SqlClient.Server.IBinarySerialize”。

So how on Earth can one use HierarchyId type in .NET Core 3.1?那么到底如何在 .NET Core 3.1 中使用 HierarchyId 类型呢?

I'm planning to host this solution on linux.我计划在 linux 上托管这个解决方案。

UPDATE更新

I do use Microsoft.Data.SqlClient 2.0 which is compatible with .NET Core.我确实使用与 .NET Core 兼容的Microsoft.Data.SqlClient 2.0。 Also, I added then EntityFrameworkCore.SqlServer.HierarchyId , and I get this error:另外,我添加了EntityFrameworkCore.SqlServer.HierarchyId ,我收到了这个错误:

System.InvalidCastException: Unable to cast object of type 'Microsoft.SqlServer.Types.SqlHierarchyId' to type 'Microsoft.Data.SqlClient.Server.IBinarySerialize'. System.InvalidCastException:无法将类型为“Microsoft.SqlServer.Types.SqlHierarchyId”的 object 转换为类型“Microsoft.Data.SqlClient.Server.IBinarySerialize”。

Here's the.csproj:这是.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更新 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. 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.您提到您使用Microsoft.Data.SqlClient ,请确保您使用与 Z303CB0EF9EDB9082D61BBBE5825D 兼容的https://www.nuget.org/packages/Microsoft.Data.SqlClient/

In case of other errors please check also Entity Framework Core hierarchyid如果出现其他错误,请同时检查Entity Framework Core hierarchyid

The solution at the moment to cast hierarchyid to NVARCHAR in all your queries:目前在所有查询中将 hierarchyid 转换为 NVARCHAR 的解决方案:

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

and then use it as string.然后将其用作字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 .NET Core 3.1 在网络上搜索所有 SQL 服务器实例 - Search for all SQL Server instances on network with .NET Core 3.1 如何使用 NLog in.Net Core 3.1 登录 SQL 服务器? - How to Logging To SQL Server With NLog in .Net Core 3.1? .NET Core 3.1 Linq 和 SQL 服务器无法转换 DateTime - .NET Core 3.1 Linq and SQL Server can not translate DateTime 尝试对 ASP.NET Core 3.1 使用多个身份验证方案时出现异常 - Exceptions when trying to use multiple authentication schemes with ASP.NET Core 3.1 NHibernatearchitectureid SQL Server 2014 - NHibernate hierarchyid SQL Server 2014 在 Asp.Net Core 3.1 中处理 SignalR 集线器中的所有异常 - Handle all exceptions in SignalR hubs in Asp.Net Core 3.1 NLog写入.Net Core 3.1中的多个日志 - NLog write to multiple logs in .Net Core 3.1 多个路由相同的动作 .NET Core 3.1 - Multiple routes same action .NET Core 3.1 Migration .NET Core 2.2 to .NET Core 3.1- LINQ LEFT JOIN - Calling Take() on DefaultIfEmpty does not translate to server side SQL - Migration .NET Core 2.2 to .NET Core 3.1- LINQ LEFT JOIN - Calling Take() on DefaultIfEmpty does not translate to server side SQL .Net Core 3.1 - 在连接 Sql Server 时卡在 Linux 中的 connection.open() - .Net Core 3.1 - Stuck on connection.open() in the Linux while connecting Sql Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM