我正在使用 Fluent NHibernate 的自动映射,并使用以下代码来确保 NHibernate 不会剥离毫秒:
public class TimestampTypeConvention : IPropertyConvention, IPropertyConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Type == typeof(DateTime) || x.Type == typeof(DateTimeOffset));
}
public void Apply(IPropertyInstance instance)
{
instance.CustomType<TimestampType>();
}
}
这工作得很好,因此数据正确地存储在数据库中。
但是,当我运行以下 LINQ 查询时,我没有得到预期的匹配:
bool isDuplicate = session.Query<TagData>()
.Any(x => x.TagName == message.EventTag.TagName
&& x.TimeStamp == message.EventTag.TimeStamp.UtcDateTime);
生成的 SQL 如下所示,并解释了为什么这不起作用:
select tagdata0_."Id" as column1_0_, tagdata0_."TagName" as column2_0_,
tagdata0_."TimeStamp" as column3_0_, tagdata0_."Value" as column4_0_,
tagdata0_."QualityTimeStamp" as column5_0_, tagdata0_."QualitySubstatus" as column6_0_,
tagdata0_."QualityExtendedSubstatus" as column7_0_, tagdata0_."QualityLimit" as column8_0_,
tagdata0_."QualityDataSourceError" as column9_0_, tagdata0_."QualityTagStatus" as column10_0_,
tagdata0_."TagType" as column11_0_ from "TagData" tagdata0_
where tagdata0_."TagName"=:p0 and tagdata0_."TimeStamp"=:p1 limit 1;
:p0 = 'VALVE_HW_CMD' [Type: String (0)],
:p1 = 01.03.2013 16:51:30 [Type: DateTime (0)]
如何强制生成的查询使用完整精度?
顺便说一句,message.EventTag.TimeStamp
是一个DateTimeOffset