【问题标题】:Fluent Nhiberhate And Missing Milliseconds流畅的 Nhiberhate 和缺少毫秒
【发布时间】:2010-05-20 13:20:42
【问题描述】:

我正在为我当前的项目使用 Fluent Nhibernate 和 Nhibernate。我需要将时间记录到毫秒。我有这个用于我的映射

            Map(x => x.SystemDateTime)
            .CustomType("Timestamp")
            .Not.Nullable();

我生成了 hbm.xml 文件,行如下:

<property name="SystemDateTime" type="Timestamp">
  <column name="SystemDateTime" not-null="true" />
</property>

我已阅读这是修复,但数据库中的记录没有毫秒。有没有人解决过这个问题。而且我也试过CustomSqlType。

谢谢

【问题讨论】:

  • 您使用的是哪个数据库,Timestamp 列的类型(在数据库中)是什么?
  • 另外,您已有的数据已被截断,该信息已丢失。
  • 我也想知道正在使用哪个数据库。例如,SQLite 不存储毫秒,需要变通方法。

标签: nhibernate fluent-nhibernate fluent-nhibernate-mapping


【解决方案1】:

我们使用与您相同的方法,它确实正确存储了毫秒。如果您不总是这样做,尽管您的旧记录会丢失它们的毫秒数。

假设您想为所有 DateTime 字段存储毫秒,您可以使用约定:

public class OurPropertyConventions : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        Type type = instance.Property.PropertyType;
        if (type == typeof(DateTime) || type == typeof(DateTime?))
            instance.CustomType("Timestamp");
    }
}

您的映射现在可以是:

Map(x => x.SystemDateTime).Not.Nullable();

【讨论】:

  • 知道这是否可以在配置中完成,即。 Fluently.Configure().Conventions.Add(...)?
  • var factory = Fluently.Configure() .Mappings(m => m.FluentMappings.Conventions.Add(new TimestampConvention())) .BuildSessionFactory();
  • @AshBurlaczenko 如何在配置中添加约定的另一个示例是var sessionFactory = Fluently.Configure(rawConfig).Database(PersistenceConfigurer).Mappings(m =&gt; m.FluentMappings.AddFromAssembly(typeof(OurPropertyConventions).Assembly).Conventions.AddFromAssemblyOf&lt;OurPropertyConventions&gt;());
【解决方案2】:

重温一下,因为以前的答案有些不完整。

假设您希望以完整详细信息和毫秒精度存储所有 DateTime 字段,请使用 TIMESTAMP(6) WITH TIME ZONE 作为 SQL 列类型。您需要 FluentNHibernate 的属性约定:

using System;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;

internal class DateTimeMapsToTimestampConvention
    : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType<NHibernate.Type.TimestampType>();
    }

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Type == typeof(DateTime)
            || x.Type == typeof(DateTime?));
    }
}

然后将此约定添加到流式配置会话工厂构建代码中:

var factory =  Fluently.Configure().Mappings(
  m => assemblies.ForEach(
    assembly => m.FluentMappings.AddFromAssembly(assembly)
                .Conventions.Add(new DateTimeMapsToTimestampConvention())))
            .BuildSessionFactory();

【讨论】:

    【解决方案3】:

    这对我不起作用:

    Map(x => x.DateCreated).Column("DATE_CREATED").CustomSqlType("TIMESTAMP(6) WITH TIME ZONE").Not.Nullable();
    

    虽然这样做了:

    Map(x => x.DateCreated).Column("DATE_CREATED").CustomType("timestamp").CustomSqlType("TIMESTAMP(6) WITH TIME ZONE").Not.Nullable();
    

    我不知道为什么:/

    【讨论】:

    • 对我来说 CustomType("timestamp") 有效,但 CustomType("Timestamp") 没有
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2011-09-09
    • 2012-06-08
    • 1970-01-01
    • 2015-08-01
    • 2021-02-26
    相关资源
    最近更新 更多