12

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?

3
  • 2
    Your problem is with EF, not WCF. The problem is that EF can't represent hierarchyid columns. Commented Nov 30, 2010 at 18:52
  • I used this little bit of code to make working with HierarchyID strings a bit easier... stackoverflow.com/questions/3347860/… Commented Feb 29, 2012 at 16:46
  • @EBarr, Your implementation looks good, my requirement was just use the existing HierarchyID as it is. The only thing i did was, used computed Column since EF don't support. By the use of computed column, its doing its job great. Commented Mar 1, 2012 at 10:14

2 Answers 2

24

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.

Sign up to request clarification or add additional context in comments.

2 Comments

As i told early, while adding the table to the EDMX file, the hierarchyId field is not appearing. So i have to have method in svc file and insert using "insert into ... " query?
OK, This can be used as getter, how about setter? i tried the setter kind of stuff. If setter is possible, everything can be done in simpler way. By using this i'm able to get the data. Thanks marc_s.
10

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.