使用SQLCE 3.5 SP2将ISO8601字符串映射到EF4中的datetimeoffset [英] Mapping of ISO8601 string to datetimeoffset in EF4 using SQLCE 3.5 SP2

查看:64
本文介绍了使用SQLCE 3.5 SP2将ISO8601字符串映射到EF4中的datetimeoffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF4和POCO以及Linq to Entities。数据库是Windows桌面上的sql ce 3.5 SP2。我首先使用模型并从模型生成数据库。我设计了一个实体模型,该实体模型在其中一个实体上具有DateTime类型的DateCreated属性。这在创建linq表达式时非常有效。

I am using EF4 with POCO and Linq to Entities. The database is sql ce 3.5 SP2 on a windows desktop. I am using model first and generates the database from the model. I have designed an entity model that has a DateCreated property of type DateTime on one of the entities. This works perfectly when creating linq expressions.

现在我的问题是我们需要将日期和时间信息存储在带有时区信息的ISO8601格式的字符串中,即&ybsp; yyyy-MM- ddTHH:mm:ss.fffzzz例如2007-12-23T23:11:15.345 + 07:00。但是,我希望实体有一个DateTimeOffset成员,以允许我的客户端轻松编写linq查询而不会出现任何字符串问题。

My problem is now that we need to store the date and time information in a string formatted as ISO8601 with timezone info, i.e. yyyy-MM-ddTHH:mm:ss.fffzzz e.g. 2007-12-23T23:11:15.345+07:00. But I want the entity to have a DateTimeOffset member to allow my clients to write linq queries easily without any string issues.

如何将db中的字符串转换为DateTimeOffset实体?要求是客户端应该能够像现在一样使用linq,例如

How can i convert the string in the db to a DateTimeOffset on the entity? The requirement is that the clients shall be able to use linq as they currently do e.g.

DateTime date = DateTime.Now;
IEnumerable < Model.Entities。 LibraryItem > items = _libraryModel.LibraryEntities.OfType< Model.Entities。 LibraryItem > () .Where( item => item.DateCreated.Year == date.Year&& item.DateCreated.Month == date.Month&& item.DateCreated.Day == date.Day

DateTime date = DateTime.Now;
IEnumerable<Model.Entities.LibraryItem> items = _libraryModel.LibraryEntities.OfType<Model.Entities.LibraryItem>().Where( item =>item.DateCreated.Year == date.Year && item.DateCreated.Month == date.Month && item.DateCreated.Day == date.Day)

有没有办法进行转换在数据库进入实体之前从数据库中获取值时完成。我查看了嵌入在emdx中的用户定义函数和存储过程,但我不知道如何继续。

Is there some way to have the conversion beeing done when the value is fetched from the database before it enters the entity. I have looked at user defined functions and stored procedures embedded in the emdx but I do not know how to continue.

祝你好运,

/ Peter

推荐答案

如果希望LINQ对此列起作用,则需要在数据库中提供它。执行此操作后,您只需要保持此属性的值与字符串的值同步,这可以使用诸如INotifyPropertyChanged之类的机制或通过侦听上下文的SavingChanged方法并修复任何LibraryItems来完成。已创建或修改...

If you want LINQ to work against this column, you need to provide it in the database. Once you do this, you simply need to keep the value of this property in sync with the value of the string, which can be done using a mechanism such as INotifyPropertyChanged or by listening in on the SavingChanged method of the context and fixing up any LibraryItems that have been created or modified...

HTH,  Noam

HTH,
 Noam


这篇关于使用SQLCE 3.5 SP2将ISO8601字符串映射到EF4中的datetimeoffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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