Need examples for Entityframework.HierarchyId

entity-framework entity-framework-6

Question

On NuGet, I discovered EF.HierarchyId. It appears that EF6.1.1 now supports it.

I'm wondering if there is any documentation about how to utilize it that I can find. For instance:

Does it support edmx? Can models with a hierarchyId column be imported?

examples of linq? What commands can I use?

I'm grateful.

1
4
8/8/2014 4:06:16 AM

Accepted Answer

It's still in progress, but I just installed it.

when first creating from a database.

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 building initially from code.

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

The database and table were properly established; however, attempting to add many objects at once results in an error.

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

    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."
    db.SaveChanges();
}

And using a key does not work for finding.

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
}
2
8/8/2014 8:52:48 AM

Popular Answer

There is no longer an ArgumentException saying that "At least one object must implement IComparable" as of version 6.1.3-alpha1. (This is identical to version 6.1.2 and only includes the solution to this issue.) However, I am unable to duplicate the "And locating by key is not working." issue.

Installing a customized version of EntityFramework Tools is required if you wish to build the model from the database. CodePlex has the source code at https://entityframework.codeplex.com/SourceControl/network/forks/zgabi/EfHierarchyId. Edmx should function; if you encounter any issues, please contact zavarkog(X)gmail.com.



Related Questions





Related

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