实体框架核心层级 ID [英] Entity Framework Core hierarchyid

查看:17
本文介绍了实体框架核心层级 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 EF Core,但我需要在我的一张表上使用 sql server 数据类型 hierarchyid.

I'm trying to use EF Core but I need to use the sql server datatype hierarchyid on one of my tables.

是否可以在我的 c# 类中指定一个字段为hierarchyid 类型?

Is it possible to designate a field in my c# class to be of type hierarchyid?

我可以在不破坏 EF 的情况下手动将列 EF 创建为 hierarchyid 类型吗?

Could I manually column EF creates to be type hierarchyid without breaking EF?

我试过这样做:

public class Entity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public SqlHierarchyId HierarchyId { get; set; }
}

因为建议 EF 核心 2.1 支持此功能,但是当我运行 add-migration 时出现此错误:

As it's suggested that this is supported by EF core 2.1, however when i run add-migration I get this error:

The current CSharpHelper cannot scaffold literals of type 'Microsoft.SqlServer.Types.SqlHierarchyId'. Configure your services to use one that can.

推荐答案

如果您使用的是 Entity Framework Core 3.1,则应使用 NuGet EntityFrameworkCore.SqlServer.HierarchyId..NET Core 不完全支持 Microsoft.SqlServer.Types.

If you are using Entity Framework Core 3.1 you should use NuGet EntityFrameworkCore.SqlServer.HierarchyId. Microsoft.SqlServer.Types is not fully supported by .NET Core.

https://www.nuget.org/packages/EntityFrameworkCore.SqlServer.HierarchyId

https://github.com/dotnet/efcore/issues/365#issuecomment-618688829

EntityFrameworkCore.SqlServer.HierarchyId 由 @bricelam 编写,在 Entity Framework 团队工作,但需要注意的是,此 NuGet 仍然是不受支持的第三方包.

EntityFrameworkCore.SqlServer.HierarchyId is written by @bricelam that works on the Entity Framework team but a thing to note is that this NuGet is still an unsupported third-party package.

我认为这仍然是您最好的选择,因为它不会很快实施并且可能永远不会得到官方支持.来自 2020-04-07 的评论:

This is still your best bet I think given that it won't be implemented any time soon and perhaps never have a official support. Comment from 2020-04-07:

基本上,我们的小团队实施似乎并不谨慎,现在支持并维护 .NET 的 HierarchyId 库,所以我们实际上被阻止执行此操作.

Basically, it doesn't seem prudent for our small team to implement, support, and maintain a HierarchyId library for .NET right now, so we're effectively blocked on implementing this.

https://github.com/dotnet/efcore/issues/365#issuecomment-610586190

Microsoft SqlClient Data Provider for SQL Server 中也存在一个问题,以支持 .NET Core 的 SqlHierarchyId 类型.(用于 SQL Server 的 Microsoft ADO.NET 驱动程序,又名 Microsoft.Data.SqlClient GitHub 存储库)

There is also an issue filed in Microsoft SqlClient Data Provider for SQL Server to support SqlHierarchyId type for .NET Core. (Microsoft ADO.NET driver for SQL Server aka the Microsoft.Data.SqlClient GitHub repository)

https://github.com/dotnet/SqlClient/issues/322

这篇关于实体框架核心层级 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆