entity-framework - 确保 DateTime 属性返回 DateTimeKind.Utc

标签 entity-framework linq-to-entities .net-4.0

是否可以在 Kind == DateTimeKind.Utc 的实体对象中定义 DateTime 属性?通过使用 .edmx 文件或 t4 模板?

如果可能使用 t4,请描述如何更改属性。目前,该属性生成为:

[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();

最佳答案

我确保所有 DateTime 值都被读取为 Utc DateTimes 的解决方案如下:

我使用了与 Michael 相同的方法(请参阅其他博客文章:https://stackoverflow.com/a/9386364/1069313),然后我进行了更深入的研究,并使用反射来搜索 DateTime 和 DateTime?

首先,我在 DbContext Extensions 方法类中编写了三个方法。因为我需要将它用于多个 DbContexts

public static void ReadAllDateTimeValuesAsUtc(this DbContext context)
{
        ((IObjectContextAdapter)context).ObjectContext.ObjectMaterialized += ReadAllDateTimeValuesAsUtc;
}

private static void ReadAllDateTimeValuesAsUtc(object sender, ObjectMaterializedEventArgs e)
{
    //Extract all DateTime properties of the object type
    var properties = e.Entity.GetType().GetProperties()
        .Where(property => property.PropertyType == typeof (DateTime) ||
                           property.PropertyType == typeof (DateTime?)).ToList();
    //Set all DaetTimeKinds to Utc
    properties.ForEach(property => SpecifyUtcKind(property, e.Entity));
}

private static void SpecifyUtcKind(PropertyInfo property, object value)
{
    //Get the datetime value
    var datetime = property.GetValue(value, null);

    //set DateTimeKind to Utc
    if (property.PropertyType == typeof(DateTime))
    {
        datetime = DateTime.SpecifyKind((DateTime) datetime, DateTimeKind.Utc);
    }
    else if(property.PropertyType == typeof(DateTime?))
    {
        var nullable = (DateTime?) datetime;
        if(!nullable.HasValue) return;
        datetime = (DateTime?)DateTime.SpecifyKind(nullable.Value, DateTimeKind.Utc);
    }
    else
    {
        return;
    }

    //And set the Utc DateTime value
    property.SetValue(value, datetime, null);
}

然后我转到我的 WebsiteReadModelContext 的构造函数,它是一个 DbContext 对象并调用 ReadAllDateTimeValuesAsUtc 方法
public WebsiteReadModelContext()
{
      this.ReadAllDateTimeValuesAsUtc();
}

关于entity-framework - 确保 DateTime 属性返回 DateTimeKind.Utc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1906331/

相关文章:

entity-framework - Entity Framework - 确保日期是将来的日期

entity-framework - Entity Framework - 连接新添加的 Poco 实体并加载子对象(插入/添加)

c# - 具有不同线程安全模式的 System.Lazy<T>

wpf - LINQ to Entities 结合了两个 IQueryable<AnonymousType>

c# - 设置文件系统权限

c# - 在 C# (.NET 4.0) 中填充和播放音频缓冲区

entity-framework - Entity Framework (v6)在异常后关闭连接而不使用 using 语句?里面的实验结果

c# - 将多个导航属性配置到同一个表时出错

c# - 使用导航属性的 LINQ 查询生成多个 SELECT 语句

c# - 集合中集合的 Linq where 子句