可能有与存储层不同的 EF 属性数据类型吗?

发布于 2024-08-26 10:01:23 字数 768 浏览 5 评论 0原文

我正在使用实体框架为 PatientEntities 构建 WCF 数据服务。

我的解决方案需要满足以下要求:

  • 实体 Patient 的属性 DateOfBirth 以字符串形式存储在 SQL Server 中。如果实体类不使用“string”类型而是使用 DateTime 类型,那将是理想的。 (我希望这是可能的,因为我们正在从存储层中抽象出来)。在哪里可以放置一个转换机制来与 DateTime/string 进行相互转换,以便实体和 SQL Server 保持同步?我无法更改存储层的结构,因此我必须解决它。
  • 需要使用 WCF 数据服务(只读,因此无需保存更改),因为客户端将能够使用 LINQ 表达式来使用该服务。他们可以根据所需的任何给定查询场景生成结果,而不受 GetPatient(int ID) 等单一方法的限制。

我尝试过使用 DTO,但遇到了将 ObjectContext 映射到 DTO 的问题,我认为这在理论上是不可能的......或者如果是的话也太复杂了。

我尝试使用自我跟踪实体,但如果我是正确的,它们需要 .edmx 文件中的元数据,并且这不允许使用不同的属性数据类型。

我还想向我的实体 getter 方法添加自定义,以便“string”类型的属性“MRN”需要在返回之前执行 .Replace("MR~", string.Empty)。我可以将其添加到 getter 方法中,但问题是实体框架下次刷新实体类时将覆盖该方法。有一个永久的地方可以放置这些吗?

我应该改用 POCO 吗?这将如何与 WCF 数据服务配合使用?该服务将在哪里获取元数据?

I am putting together a WCF Data Service for PatientEntities using Entity Framework.

My solution needs to address these requirements:

  • Property DateOfBirth of entity Patient is stored in SQL Server as string. It would be ideal if the entity class did not also use the "string" type but rather a DateTime type. (I would expect this to be possible since we're abstracting away from the storage layer). Where could a conversion mechanism be put in place that would convert to and from DateTime/string so that the entity and SQL Server are in sync?. I cannot change the storage layer's structure, so I have to work around it.
  • WCF Data Services (Read-only, so no need for saving changes) need to be used since clients will be able to use LINQ expressions to consume the service. They can generate results based on any given query scenario they need and not be constrained by a single method such as GetPatient(int ID).

I've tried to use DTOs, but run into problem of mapping the ObjectContext to a DTO, I don't think that is theoretically possible...or too complicated if it is.

I've tried to use Self Tracking Entities but they require the metadata from the .edmx file if I'm correct, and this isn't allowing a different property data type.

I also want to add customizations to my Entity getter methods so that a property "MRN" of type "string" needs to have .Replace("MR~", string.Empty) performed before it is returned. I can add this to the getter methods but the problem with that is Entity Framework will overwrite that next time it refreshes the entity classes. Is there a permanent place I can put these?

Should I use POCO instead? How would that work with WCF Data Services? Where would the service grab the metadata?

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

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

发布评论

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

评论(1

離人涙 2024-09-02 10:01:23

这绝对是可能的。您需要使用 QueryView,它可以让您控制给定列如何映射到实体上的属性。例如,您可以对患者实体执行以下操作。

<EntitySetMapping Name="Patients">
<QueryView>
select value conceptualnamespace.Patient(p.PatientId,
cast(p.DateOfBirth as Edm.DateTime), 
replace(p.Name,'MR~','')
from entitycontainer.Patients as p
</QueryView>
</EntitySetMapping>

我在书中详细介绍了这个概念。食谱被称为。
15-2。将实体映射到一个或多个表的自定义部分

This is definitely possible. What you need to use is QueryView which lets you control how a given column maps to a property on your entity. for instance here is something u could do on patients entity.

<EntitySetMapping Name="Patients">
<QueryView>
select value conceptualnamespace.Patient(p.PatientId,
cast(p.DateOfBirth as Edm.DateTime), 
replace(p.Name,'MR~','')
from entitycontainer.Patients as p
</QueryView>
</EntitySetMapping>

i cover more on this concept in my book. the recipe is called.
15-2. Mapping an Entity to Customized Parts of One or More Tables

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