Need examples for Entityframework.HierarchyId

entity-framework entity-framework-6


I found EF.HierarchyId on NuGet. Looks like EF6.1.1 supports it now?

Wondering if there are any documentation I can see about how to use it. For example:

-does it work with edmx? may it import models with hierarchyId column?

-some linq examples? what commands are supported?

Thank you.

8/8/2014 4:06:16 AM

Accepted Answer

I just installed it, I think it's still under development.

When creating from database first.

The data type 'hierarchyid' is currently not supported for the target .NET Framework version; the column 'Id' in table 'dbo.Users' was excluded.

The column 'Id' on the table/view 'dbo.Users' was excluded, and is a key column. The table/view has been excluded. Please fix the entity in the schema file, and uncomment.

When creating from code first.

public class AppContext : DbContext
    public DbSet<User> Users { get; set; }
public class User
    public HierarchyId Id { get; set; }
    public string Name { get; set; }

It worked, the database and the table were successfully created, but adding multiple objects at once throws error.

using (var db = new AppContext())
    db.Users.Add(new User { Id = HierarchyId.Parse("/"), Name = "President" });
    // Working.

    db.Users.Add(new User { Id = HierarchyId.Parse("/1/"), Name = "VP 1" });
    db.Users.Add(new User { Id = HierarchyId.Parse("/2/"), Name = "VP 2" });
    db.Users.Add(new User { Id = HierarchyId.Parse("/3/"), Name = "VP 3" });
    // ArgumentException "At least one object must implement IComparable."

And finding by key is not working.

using (var db = new AppContext())
    var id = HierarchyId.Parse("/");
    var user1 = db.Users.Find(id); // null
    var user2 = db.Users.FirstOrDefault(u => u.Id == id); // null
    var user3 = db.Users.FirstOrDefault(u => HierarchyId.Compare(u.Id, id) == 0); // null
    var user4 = db.Users.AsNoTracking().ToArray()
        .FirstOrDefault(u => u.Id == id); //not null
    var user5 = db.Users.AsNoTracking().ToArray()
        .FirstOrDefault(u => HierarchyId.Compare(u.Id, id) == 0); //not null
8/8/2014 8:52:48 AM

Popular Answer

From version 6.1.3-alpha1 you can add multiple entities, no more ArgumentException "At least one object must implement IComparable." (This is the same as version 6.1.2, only contains the fix for this problem) But I can't reproduce the "And finding by key is not working." problem.

If you want to create the model from database you need to install a custom version of EntityFramework Tools. The source code is on CodePlex: Edmx should work, if you find any problem, please report it to zavarkog(X)

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow