Skip to content

efcore/EFCore.SqlServer.HierarchyId

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code
This branch is 65 commits ahead, 1 commit behind aljones:master.

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.

EntityFrameworkCore.SqlServer.HierarchyId

build status latest version downloads license

Adds hierarchyid support to the SQL Server EF Core provider.

Installation

The latest stable version is available on NuGet.

dotnet add package EntityFrameworkCore.SqlServer.HierarchyId

Compatibility

The following table show which version of this library to use with which version of EF Core.

EF Core Version to use
7.0 4.x
6.0 3.x
3.1 1.x

Usage

Enable hierarchyid support by calling UseHierarchyId inside UseSqlServer. UseSqlServer is is typically called inside Startup.ConfigureServices or OnConfiguring of your DbContext type.

options.UseSqlServer(
    connectionString,
    x => x.UseHierarchyId());

Add HierarchyId properties to your entity types.

class Patriarch
{
    public HierarchyId Id { get; set; }
    public string Name { get; set; }
}

Insert data.

dbContext.AddRange(
    new Patriarch { Id = HierarchyId.GetRoot(), Name = "Abraham" },
    new Patriarch { Id = HierarchyId.Parse("/1/"), Name = "Isaac" },
    new Patriarch { Id = HierarchyId.Parse("/1/1/"), Name = "Jacob" });
dbContext.SaveChanges();

Query.

var thirdGeneration = from p in dbContext.Patriarchs
                      where p.Id.GetLevel() == 2
                      select p;

Testing

A package for the In-Memory EF Core provider is also available to enable unit testing components that consume HierarchyId data.

dotnet add package EntityFrameworkCore.InMemory.HierarchyId
options.UseInMemoryDatabase(
    databaseName,
    x => x.UseHierarchyId());

See also

About

Adds hierarchyid support to the SQL Server EF Core provider

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%