Nhibernate Fluent Nhiberhate和缺失毫秒
我正在为我当前的项目使用Fluent Nhibernate和Nhibernate。我需要把时间记录到毫秒。这是我的地图Nhibernate Fluent Nhiberhate和缺失毫秒,nhibernate,fluent-nhibernate,fluent-nhibernate-mapping,Nhibernate,Fluent Nhibernate,Fluent Nhibernate Mapping,我正在为我当前的项目使用Fluent Nhibernate和Nhibernate。我需要把时间记录到毫秒。这是我的地图 Map(x => x.SystemDateTime) .CustomType("Timestamp") .Not.Nullable(); 我生成了hbm.xml文件,行如下所示: <property name="SystemDateTime" type="Timestamp"> &l
Map(x => x.SystemDateTime)
.CustomType("Timestamp")
.Not.Nullable();
我生成了hbm.xml文件,行如下所示:
<property name="SystemDateTime" type="Timestamp">
<column name="SystemDateTime" not-null="true" />
</property>
我已经读到这是修复,但数据库中的记录没有毫秒。有人解决了这个问题吗。我也尝试过CustomSqlType
谢谢我们使用与您相同的方法,它确实正确地存储了毫秒数。如果你不总是这样做,尽管你的旧记录会丢失毫秒 假设要为所有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();
这对我不起作用:
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();
我不知道为什么:/再次讨论这个问题,因为之前的答案有点不完整 假设希望所有DateTime字段都以完整的详细信息和毫秒精度存储,请使用带时区的时间戳(6)作为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?));
}
}
您正在使用哪个数据库,时间戳列的类型(在数据库中)是什么?另外,您已经拥有的数据已被截断,并且该信息已丢失。我还想知道正在使用哪个数据库。例如,SQLite不存储毫秒,需要解决方法。您知道这是否可以在配置中完成吗,例如,
fluent.Configure().Conventions.Add(…)
?var factory=fluent.Configure().Mappings(m=>m.FluentMappings.Conventions.Add(new TimestampConvention()).BuildSessionFactory()@AshBurlaczenko关于如何向配置中添加约定的另一个示例是var sessionFactory=fluent.Configure(rawConfig).Database(PersistenceConfigurer).Mappings(m=>m.FluentMappings.AddFromAssembly(typeof(OurPropertyConventions.Assembly))代码>对我来说,CustomType(“时间戳”)起作用,但CustomType(“时间戳”)没有
var factory = Fluently.Configure().Mappings(
m => assemblies.ForEach(
assembly => m.FluentMappings.AddFromAssembly(assembly)
.Conventions.Add(new DateTimeMapsToTimestampConvention())))
.BuildSessionFactory();