Entity Framework Core 8 Preview 2 Released

MMS Founder
MMS Alen Genzic

Article originally posted on InfoQ. Visit InfoQ

Entity Framework Core 8 Preview 2 was released on March 14th. The most notable feature in EF Core 8 preview 2 is support for SQL Server hierarchical data.

The EntityFrameworkCore.SqlServer.HierarchyId package is an unofficial way to include the usage of hierarchical data into Entity Framework and has been available for a few years already (version 1.0.0 of the package has been available from April 2020), however in EF Core 8 preview 2, this feature has an official implementation, which is based on this community package. The new official package is Microsoft.EntityFrameworkCore.SqlServer.HierarchyId.

HierarchyId is enabled by installing the aforementioned package and the following code to your application startup code

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

After installing and configuring the HierarchyId feature, you can use it to represent hierarchical data such as organizational structures, folder structures and web site page tree structures.

On the entity type itself, the type is used as any other property type:

public HierarchyId NodePath { get; set; }

The type represents an entities path in a tree structure. For example, for a node with the path /1/2/3:

  • / is the root of the tree,
  • 1 is the grandparent
  • 2 is the parent of the node
  • 3 is the identifier for the node itself

Taking this same example, the paths /1/2/4 and /1/2/5 are siblings of our initial example node, whilst /1/3 and /1/4 are siblings to the nodes parent. Nodes can also be inserted between two other nodes by using decimal values. The node /1/3.5 is between the nodes /1/3 and /1/4.

Whilst this format is human readable in code, SQL Server itself uses a compact binary format to store this identifier (varbinary).

The type also has some limitations in SQL Server directly:

  • A SQL Server hierarchyId doesn’t by itself represent a tree structure. It is the responsibility of the application to assign hierarchyId values so that the relationships between rows in a table represent a tree
  • There is no guarantee that a hierarchyId will be unique, thus it is also the applications responsibility to ensure appropriate concurrency control
  • There is no foreign key constraint on the values of hierarchyId. For example, the application should make sure all descendants of a node in the hierarchy have their hierarchyId’s updated upon deletion of their parent.
  • EF Core 7 introduced the JSON Columns support for SQL Server and in this version, that feature is extended to support SQLite databases as well.

EF8 previews can currently be used in .NET 6 LTS and .NET 7. The EF8 release is aligned with the next LTS version of .NET 8, scheduled for release in November 2023.

About the Author

Subscribe for MMS Newsletter

By signing up, you will receive updates about our latest information.

  • This field is for validation purposes and should be left unchanged.