Arn*_*sen 17 nhibernate datetime utc
有没有一种流畅的nhibernate映射DateTime来重新水化我的实体,DateTime.Kind设置为Utc而不是未指定?我目前正在坚持使用Utc的DateTime,但是Kind返回时总是未指定,浪费我的时间.
Dav*_*den 37
从Nhibernate 3.0开始,使用FluentNHibernate,您可以执行以下操作:
Map(x => x.EntryDate).CustomType<UtcDateTimeType>();
Run Code Online (Sandbox Code Playgroud)
无需再使用拦截器.
g .*_*g . 12
这不是特定于流畅的,但是NHibernate映射的基础.我们使用拦截器来指定Kind.它与此博客文章中的方法类似,列出了几个替代方案.还有一个建议的补丁(NH-1135)用于本地处理UtcDateTime和LocalDateTime.我鼓励你投票支持它.
public class InterceptorBase : EmptyInterceptor
{
public override bool OnLoad(object entity, object id, object[] state,
string[] propertyNames, IType[] types)
{
ConvertDatabaseDateTimeToUtc(state, types);
return true;
}
private void ConvertDatabaseDateTimeToUtc(object[] state, IList<IType> types)
{
for (int i = 0; i < types.Count; i++)
{
if (types[i].ReturnedClass != typeof(DateTime))
continue;
DateTime? dateTime = state[i] as DateTime?;
if (!dateTime.HasValue)
continue;
if (dateTime.Value.Kind != DateTimeKind.Unspecified)
continue;
state[i] = DateTime.SpecifyKind(dateTime.Value, DateTimeKind.Utc);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5526 次 |
| 最近记录: |