Datetime Fluent Nhibernate-将所有日期配置为从UTC重新水化

Datetime Fluent Nhibernate-将所有日期配置为从UTC重新水化,datetime,nhibernate,configuration,fluent-nhibernate,utc,Datetime,Nhibernate,Configuration,Fluent Nhibernate,Utc,这个问题就是这么说的 该问题后面的一个答案是: Map(x => x.EntryDate).CustomType<UtcDateTimeType>(); Map(x=>x.EntryDate).CustomType(); 对于一个实体上的一个属性有效 我想知道是否有办法指定所有datetime属性在数据库中存储为UTC 这可能吗?如果可能,如何实现?使用fluent NHibernate的方式是惯例 詹姆斯·格雷戈里于2012年4月3日编辑了本页 … 这些约定是使用一组接口

这个问题就是这么说的

该问题后面的一个答案是:

Map(x => x.EntryDate).CustomType<UtcDateTimeType>();
Map(x=>x.EntryDate).CustomType();
对于一个实体上的一个属性有效

我想知道是否有办法指定所有datetime属性在数据库中存储为UTC


这可能吗?如果可能,如何实现?

使用fluent NHibernate的方式是
惯例

詹姆斯·格雷戈里于2012年4月3日编辑了本页


这些约定是使用一组接口和基类构建的,每个接口和基类定义一个方法Apply,并根据您创建的约定类型使用不同的参数;此方法用于更改映射。

起草的示例:

public class UtcConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (instance.Type.Name == "Date")
        {
            instance.CustomType<UtcDateTimeType>();
        }
    }
}

嗨,谢谢你的回答,拉迪姆

我必须对您的代码进行一些小的更改,以使其正常工作并支持nullableDateTime?属性

    public class UtcConvention : IPropertyConvention  {
        public void Apply(IPropertyInstance instance) {
            if (instance.Type.Name == "DateTime" || instance.Type.ToString().StartsWith("System.Nullable`1[[System.DateTime")) {
                instance.CustomType<UtcDateTimeType>();
            }
        }
    }
公共类UTC惯例:IPropertyConvention{
公共无效应用(IPropertyInstance实例){
if(instance.Type.Name==“DateTime”| | instance.Type.ToString().StartsWith(“System.Nullable`1[[System.DateTime”)){
CustomType();
}
}
}
也许这可以帮助其他人寻找解决方案

讨厌魔法字符串

using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
using NHibernate.Type;
using System;

namespace MyAwesomeApp
{
    public class UTCDateTimeConvention : IPropertyConvention
    {
        public void Apply(IPropertyInstance instance)
        {
            var type = instance.Type.GetUnderlyingSystemType();

            if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                instance.CustomType<UtcDateTimeType>();
            }
        }
    }
}
使用FluentNHibernate.Conventions;
使用FluentNHibernate.Conventions.Instances;
使用NHibernate.Type;
使用制度;
名称空间MyAwesomeApp
{
公共类UTCDateTimeConvention:IPropertyConvention
{
公共无效应用(IPropertyInstance实例)
{
var type=instance.type.GetUnderlineingSystemType();
if(type==typeof(DateTime)| type==typeof(DateTime?)
{
CustomType();
}
}
}
}

这很管用,我的朋友。非常优雅的解决方案。谢谢。很高兴看到这一点,真的。享受精彩的NHibernate,先生;)
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
using NHibernate.Type;
using System;

namespace MyAwesomeApp
{
    public class UTCDateTimeConvention : IPropertyConvention
    {
        public void Apply(IPropertyInstance instance)
        {
            var type = instance.Type.GetUnderlyingSystemType();

            if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                instance.CustomType<UtcDateTimeType>();
            }
        }
    }
}