确保日期时间属性返回DateTimeKind.Utc [英] Making sure that DateTime properties return DateTimeKind.Utc

查看:218
本文介绍了确保日期时间属性返回DateTimeKind.Utc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能定义DateTime的特性​​在实体对象是类== DateTimeKind.Utc 通过使用该.edmx文件,或T4模板?

在可能使用T4,请说明如何更改属性。目前,该物业被作为生成的:

  [EdmScalarPropertyAttribute(EntityKeyProperty =假,ISNULLABLE = FALSE)]
[DataMemberAttribute()]
大众全球:: System.DateTime的创建
{
    得到
    {
        返回_created;
    }
    内部设置
    {
        OnCreatedChanging(值);
        ReportPropertyChanging(创建);
        _created = StructuralObject.SetValidValue(值);
        ReportPropertyChanged(创建);
        OnCreatedChanged();
    }
}
私人全球:: System.DateTime的_created;
部分无效OnCreatedChanging(全球:: System.DateTime,它的价值);
部分无效OnCreatedChanged();
 

解决方案

对于我们来说,这是不切实际的始终指定DateTimeKind的规定previously:

 日期时间utcDateTime = DateTime.SpecifyKind(databaseDateTime,DateTimeKind.Utc);
 

如果要强制所有对象的DateTime出来的数据库被指定为UTC,你将需要添加一个T4转换文件,并添加额外的逻辑为所有日期时间和日期时间可为空的对象,他们得到的DateTimeKind初始化

。 UTC

我有一个博客帖子,说明此项一步一步:<一href="http://www.aaroncoleman.net/post/2011/06/16/Forcing-Entity-Framework-to-mark-DateTime-fields-at-UTC.aspx">http://www.aaroncoleman.net/post/2011/06/16/Forcing-Entity-Framework-to-mark-DateTime-fields-at-UTC.aspx

在短期:

1)创建.TT文件为您的.edmx模型

2)打开.TT文件,找到WritePrimitiveTypeProperty的方法。

3)更换现有的二传手code。这之间的一切的 ReportPropertyChanging ReportPropertyChanged 回调方法有以下内容:

 &LT;#+ IF(((PrimitiveType)primitiveProperty.TypeUsage.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.DateTime)
            {
#&GT;
        如果(小于#= code.FieldName(primitiveProperty)#&GT; ==新的日期时间())
        {
            &LT;#= code.FieldName(primitiveProperty)#&GT; = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#&GT;);
&LT;#+
            如果(ef.IsNullable(primitiveProperty))
            {
#&GT;
            如果(值!= NULL)
                &LT;#= code.FieldName(primitiveProperty)#&GT; = DateTime.SpecifyKind(小于#= code.FieldName(primitiveProperty)#&GT; .value的,DateTimeKind.Utc);
&LT;#+}
            其他
            {#&GT;
            &LT;#= code.FieldName(primitiveProperty)#&GT; = DateTime.SpecifyKind(小于#= code.FieldName(primitiveProperty)#&gt;中DateTimeKind.Utc);
&LT;#+
            }
#&GT;
        }
        其他
        {
            &LT;#= code.FieldName(primitiveProperty)#&GT; = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#&GT;);
        }
&LT;#+
        }
        其他
        {
#&GT;
    &LT;#= code.FieldName(primitiveProperty)#&GT; = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#&GT;);
&LT;#+
        }
#&GT;
 

Is it possible to define DateTime properties in entity objects that are of Kind == DateTimeKind.Utc by using either the .edmx file, or a t4 template?

When possible using t4, please describe how to change the property. Currently the property gets generated as:

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.DateTime Created
{
    get
    {
        return _created;
    }
    internal set
    {
        OnCreatedChanging(value);
        ReportPropertyChanging("Created");
        _created = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("Created");
        OnCreatedChanged();
    }
}
private global::System.DateTime _created;
partial void OnCreatedChanging(global::System.DateTime value);
partial void OnCreatedChanged();

解决方案

For our case it was impractical to always specify the DateTimeKind as stated previously:

DateTime utcDateTime = DateTime.SpecifyKind(databaseDateTime, DateTimeKind.Utc);

If you want to force all DateTime objects coming out of the database to be specified as UTC you'll need to add a T4 transform file and add additional logic for all DateTime and nullable DateTime objects such that they get initialized as DateTimeKind.Utc

I have a blog post which explains this step by step: http://www.aaroncoleman.net/post/2011/06/16/Forcing-Entity-Framework-to-mark-DateTime-fields-at-UTC.aspx

In short:

1) Create the .tt file for your .edmx model

2) Open the .tt file and find the "WritePrimitiveTypeProperty" method.

3) Replace the existing setter code. This is everything between the ReportPropertyChanging and the ReportPropertyChanged method callbacks with the following:

<#+ if( ((PrimitiveType)primitiveProperty.TypeUsage.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.DateTime)
            {
#>
        if(<#=code.FieldName(primitiveProperty)#> == new DateTime())
        {
            <#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
<#+ 
            if(ef.IsNullable(primitiveProperty))
            {  
#>              
            if(value != null)
                <#=code.FieldName(primitiveProperty)#> = DateTime.SpecifyKind(<#=code.FieldName(primitiveProperty)#>.Value, DateTimeKind.Utc);
<#+             } 
            else
            {#>
            <#=code.FieldName(primitiveProperty)#> = DateTime.SpecifyKind(<#=code.FieldName(primitiveProperty)#>, DateTimeKind.Utc);                
<#+ 
            } 
#>
        }
        else
        {
            <#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
        }
<#+ 
        }
        else
        {
#>
    <#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
<#+ 
        }
#>

这篇关于确保日期时间属性返回DateTimeKind.Utc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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