Entity Framework Core hierarchyid

10

我想使用EF Core,但是我需要在其中一个表格中使用sql server数据类型hierarchyid

是否可以将我的C#类中的某个字段指定为hierarchyid类型?

我能否手动将EF创建的列更改为hierarchyid类型而不破坏EF?

编辑: 我已经尝试过:

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

虽然EF Core 2.1支持此功能,但是当我运行add-migration时,出现了以下错误:

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

4
似乎这是一个未解决的问题:https://github.com/aspnet/EntityFrameworkCore/issues/365 - Dan Rayson
3个回答

10

更新时间2023-02-17:

HierarchyId已经是EF Core 8.0.0-preview2的一个已关闭问题。这个年底发布的8.0.0版本将由EF Core团队提供支持。

https://github.com/dotnet/efcore/milestone/162?closed=1

https://github.com/dotnet/efcore/commit/a00a518c50da4d7d6be4b215c4fdab7ce5cf6449

https://github.com/efcore/EFCore.SqlServer.HierarchyId/issues/113#event-8549485427

原文:

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

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

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

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

考虑到它可能不会很快或者永远得到官方支持,我认为这仍然是你最好的选择。2020-04-07的评论:

基本上,我们的小团队没有实现、支持和维护.NET的HierarchyId库的必要性,所以我们在实现这个问题上被阻止了。

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

Microsoft SqlClient Data Provider for SQL Server也有一个问题来支持.NET Core的SqlHierarchyId类型。(Microsoft ADO.NET driver for SQL Server即Microsoft.Data.SqlClient GitHub存储库)

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


6
对于那些寻找SqlHierarchyId包的Nuget包的人,它在Microsoft.SqlServer.Types中。
请注意,该包信息显示此包包含DbGeography和DbGeometery类型,但它还包含SqlHierarchyId类型。
还请注意,类是SqlHierarchyId,而不是HierarchyId(就像我在寻找的那样)。

1
我在我的模型中添加了一个该类型(SqlHierarchyId)的属性。然后在添加迁移时出现了以下错误:“无法从程序集System.Data加载类型Microsoft.SqlServer.Server.IBinarySerialize”。 - user3664254
1
尝试使用 dotMorten.Microsoft.SqlServer.Types,它有适当的重新实现。 - Bon

2

目前本地支持仍不完善,但截至2019年11月25日,我发现最好的方法是使用NuGet包dotMorten.Microsoft.SqlServer.Types代替Microsoft.SqlServer.Types,因为Microsoft.SqlServer.Types与Core不完全兼容。

以下是使用dotMorten生成具有hierarachyId的表格的最小工作代码示例:

请注意,使用仍然是Microsoft.SqlServer.Types,dotMortan使用相同的命名空间使其成为一个可替换的组件。

using Microsoft.EntityFrameworkCore;
using Microsoft.SqlServer.Types;

namespace ConsoleApp1
{
    public class Db : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true");
            }
        }
        public DbSet<Person> People { get; set; }
    }
    public class Person
    {
        public int Id { get; set; }
        public SqlHierarchyId HId { get; set; }
        public string Name { get; set; }
    }
}

现在add-migrationsupdate-database将起作用并正确生成表格,但是像这样的代码:

var peopleDesdenctsOfFirstPerson = db.People.Where(x => x.HId.IsDescendantOf(db.People.First().HId).IsTrue); 仍然无法正常工作,我还没有找到解决方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接