【问题标题】:Fluent NHibernate Automapping: Alter DateTime to TimestampFluent NHibernate 自动映射:将 DateTime 更改为时间戳
【发布时间】:2010-10-28 00:19:14
【问题描述】:

我正在(有点)深入使用 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 个类需要这种精度,我可以写三遍。特别是因为所有其他属性的映射仍然可以完美运行,并且以下代码仅替换了我想要的一个属性...非常好!

如果有人知道更通用的方法,请随时添加,但现在,我很高兴!

我的案例代码是:

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

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate automapping


    【解决方案1】:

    为了扩展 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>();
        }
    }
    

    【讨论】:

    • 要扩展对可为空的 DateTime 的支持并了解如何将约定添加到配置中,请参阅 this answer。我更喜欢这个答案中的Apply() 实现。请注意,如果您不使用自动映射但有专用的映射类,这两个答案也适用。
    • 你真的应该使用criteria.Expect(x =&gt; x.Type == typeof(DateTime) || x.Type == typeof(DateTime?));
    【解决方案2】:

    您可以覆盖特定的类。但不确定如何在更一般的层面上执行此操作。

    public class AlbumMappingOverride : IAutoMappingOverride<Album>
    {
        public void Override(AutoMap<Album> mapping)
        {
            mapping.Map(x => x.Created).CustomSqlTypeIs("timestamp");
        }
    }
    

    【讨论】:

      【解决方案3】:

      Danie 的答案是可以使用的,尽管需要对 Apply() 方法进行一些小改动才能使其工作:CustomSqlType("timestamp") -> CustomType("Timestamp")。我验证了这一点,因为我刚刚在自己的项目中实施了该解决方案。

      注意:我尝试对 Daniel 上面的答案进行修改,但由于更改太小,所以无法提交修改,因此这里是更正后的代码:

      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("Timestamp");
          }
      }
      

      此外,鉴于我正在写一篇新文章,这里有一个精简的示例,说明如何使用 Fluent NHibernate 创建一个使用约定的会话工厂,以帮助可能不知道的其他人:

      var factory = Fluently.Configure()
              .Mappings(m => m.FluentMappings.Conventions.Add(new TimestampConvention()))
              .BuildSessionFactory();
      

      感谢this 的帖子让我意识到这一点,因为我在网上看到很多其他帖子都有同样的问题。

      【讨论】:

      • 你真的应该使用criteria.Expect(x =&gt; x.Type == typeof(DateTime) || x.Type == typeof(DateTime?));
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 2011-06-18
      相关资源
      最近更新 更多