c# - Fluent NHibernate 自动映射 : Alter DateTime to Timestamp

标签 c# nhibernate fluent-nhibernate automapping

我正在(稍微)深入了解使用 NHibernate 的流畅界面进行自动映射。非常好的东西,但我遇到了 DateTimes 的一个小问题。我需要将数据格式更改为时间戳,否则 NHibernate 会截断毫秒数。

我找到了几个信息来源,最好的一个是: AutoMapping Info 1他正在更改属性的列名和类型。问题是,根据 this document,流畅的自动映射发生了变化。 .

现在我不知道如何让自动映射“改变类型”。我尝试了以下代码,但我被卡住了。同样,我想做的只是告诉自动映射器:

使用 DateTime 的时间戳以防止在使用自动映射时截断毫秒。

有人知道吗?到目前为止的代码:

   public class DateTimeToTimestamp : IClassConvention  
{  
    public bool Accept(IClassMap target)
    {
        return target.GetType() == typeof(DateTime);
    }

    public void Apply(IClassMap target)
    {
        throw new NotImplementedException();
    }
}

好的, 非常感谢您的回答...那样对我来说已经足够安慰了。如果我真的有 3 个类需要这种精度,我可以处理写 3 次。特别是因为所有其他属性的映射仍然完美,下面的代码只替换了我想要的一个属性......非常好!

如果有人知道更通用的方法,请随意添加,但就目前而言,我很高兴!

我的案例代码是:

    public class DateTimeToTimestamp : IAutoMappingOverride<CustomTime>
{
    public void Override(AutoMap<CustomTime> mapping)
    {
        mapping.Map(x => x.ScanDate).CustomTypeIs("timestamp");
    }
}

最佳答案

要扩展 Derek 的答案,以便在更一般的层面上进行,您可以使用自动映射约定:

public class TimestampTypeConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Type == typeof (DateTime));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType<TimestampType>();
    }
}

关于c# - Fluent NHibernate 自动映射 : Alter DateTime to Timestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/894264/

相关文章:

c# - Fluent-NHibernate 多对多级联不填充链接表

c# - LuaInterface - 一个将返回 LuaTable 值的函数

nhibernate - 多对多和HQL批量删除

mysql - 获取下一个id

nhibernate - NullReferenceException 使用 Fluent NHibernate 自动映射映射枚举集合

c# - NHibernate:保存 transient 实例时如何更新身份标识?

c# - 此事件处理程序代码会导致内存泄漏吗?

c# - 当查询不为空时,自动映射器投影返回空列表

c# - 如何 string.Join GetHostAddresses 调用的结果?

c# - 为什么我的 NHibernate 查询这么慢?