实体框架中的 HierarchyID 不起作用

发布于 2024-10-04 20:00:48 字数 240 浏览 0 评论 0原文

我们正在为我们的应用程序使用基于实体框架模型的 WCF 数据服务。

在此,我们需要添加具有 HierarchyId 类型的列的表。当我将该表添加到 EDMX 文件时,HierarchId 列没有出现在类文件中。

我应该怎么做才能使用 HierarchyID?我读到实体框架不支持 HierarchyID,那么我该如何实现这一点呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2024-10-11 20:00:48

您始终可以将 HierarchyId 转换为其字符串表示形式(例如 /1/3/4/1),然后通过 WCF 数据服务发送该字符串。

更新:如果您将这个计算的持久列添加到您的 SQL Server 表中,该新列肯定会出现在您的 EF 模型中,并且您应该能够使用它通过 WCF 和 WCF 将其发送回数据服务:

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

更新 #2:阅读文档!您可以将 /1/3/4/1 之类的字符串解析回 HierarchyId 类型 - 可以使用 HierarchyId::Parse(string) 或通常的 CAST(string as HierarchyId) 方法来执行此操作。

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.

夜访吸血鬼 2024-10-11 20:00:48

如果您使用计算列,请记住,您还需要在您的属性上使用 DatabaseGenelated 数据注释,如下所示:

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

查看本文以获取更多信息:
实体框架代码优先计算属性

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文