Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HierarchyID in Entity Framework not working

We are using WCF Data Service based on an Entity Framework model for our application.

In this we need to add the table with a column of type HierarchyId. When I add that table to the EDMX file, the HierarchId column is not appearing in the class file.

What should I do to make use of HierarchyID? I read that Entity Framework is not supporting HierarchyID, so how can I achieve this?

like image 391
Mohanavel Avatar asked Nov 30 '10 16:11

Mohanavel


2 Answers

You can always convert a HierarchyId to its string representation - something like /1/3/4/1 - and send that string across the WCF data service.

Update: if you add this computed, persisted column to your SQL Server table, that new column should definitely show up in your EF model and you should be able to use this to send it back over WCF and WCF Data Services:

ALTER TABLE dbo.YourTable
ADD HierarchyString AS (your hierarchyID field).ToString() PERSISTED

Update #2: read the docs! You can parse back a string like /1/3/4/1 into a HierarchyId type - either use the HierarchyId::Parse(string) or the usual CAST(string as HierarchyId) methods to do so.

like image 188
marc_s Avatar answered Oct 23 '22 05:10

marc_s


If you use the computed column just keep in mind that you will also need the DatabaseGenerated data annotation on your property like this:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string HierarchyString { get; set; }

Check this article out for more info: Entity Framework Code First Computed Properties

like image 36
Eric Avatar answered Oct 23 '22 03:10

Eric