通过WCF数据服务问题解决DateTimeOffset最佳工作 [英] Best work around to fix DateTimeOffset over WCF Data Service issue

查看:100
本文介绍了通过WCF数据服务问题解决DateTimeOffset最佳工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为实体框架模型创建一个WCF数据服务,其中包含一些类型为DateTimeOffeset的属性。然而,WCF数据服务不支持类型DateTimeOffset,因为我发现在搜索异常文本后属性CreationTime类型任务是类型'DateTimeOffset'不是支持的基本类型'。请参阅服务器日志更多细节的例外堆栈跟踪是... ...

I try to create a WCF Data Service for an Entity Framework model, which contains some attributes of type DateTimeOffeset. However the WCF Data Service does not support the type DateTimeOffset as I found out after googling the exception text "The property 'CreationTime' on type 'Task' is of type 'DateTimeOffset' which is not a supported primitive type.'. See server logs for more details. The exception stack trace is: ...".

我正在考虑不同的方法来解决这个问题,包括:

I'm now considering different approaches to solve this problem including:


  1. 将类型更改为可以映射到数据库中的DateTime(最坏的解决方案)

  1. change the type to something that can be mapped to DateTime in the database (worst solution)

将数据库中的列类型保留为DateTimeOffset,将列映射到Entity Framework模型中的两个属性,一个DateTime和一个额外的Offset属性,类型为integer。

Leave the column type in the database as DateTimeOffset, Map the column to two attributes in the Entity Framework model, one DateTime and an additional "Offset" attribute of type integer.

我真的不喜欢任何这些方法。有没有人找到一个很好的工作来解决这个问题?

I really don't like any of these approaches. Has anyone found a good work around to fix this issue?

推荐答案

只需添加DateTimeOffset类型作为EF数据契约的KnownType其中包含CreationTime属性,如 http://msdn.microsoft.com/ EN-US /库/ ms730167.aspx

Just add the DateTimeOffset type as a KnownType to the EF data contract that contains the CreationTime property, like explained in http://msdn.microsoft.com/en-us/library/ms730167.aspx.

DateTimeOffset是实际上被作为原语处理的复杂.NET类型之一,除了默认情况下不将其注册为序列化器的KnownType。所以您需要手动执行。

DateTimeOffset is one of the complex .NET types that is actually being handled as a primitive, except for that it is not registered by default as a KnownType to the serializers. So you need to do this manually.

您的代码可能如下所示:

Your code could look like this:

[DataContract]
[KnownType(typeof(DateTimeOffset))]
public class Task
{
    [DataMember]
    private DateTimeOffset CreationTime;
...

这篇关于通过WCF数据服务问题解决DateTimeOffset最佳工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆