[英].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 上托管这个解决方案。
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.到目前为止没有运气。
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.