SQLite将不匹配从数据库中检索的C#DateTime

时间:2011-03-16 19:16:02

标签: c# nhibernate sqlite

我正在尝试在内存中使用SQLite对我的应用程序运行一些单元测试,但我遇到了一个奇怪的问题:

我有两个问题。第一个结果是给定列表名称的最新价格表的日期,并且在第二个查询中使用DateTime来获取最近的价格。问题是,第二个查询没有返回任何结果。

知道后台可能出现什么问题吗?

        var effective = DbSession.Current.CreateCriteria<ItemPrice>()
                .SetProjection(Projections.Max("Effective"))
                .Add(Restrictions.Le("Effective", workDate))
                .CreateCriteria("PriceList")
                .Add(Restrictions.Eq("ListName", listName))
                .Add(Restrictions.Eq("Active", true))
                .UniqueResult<DateTime>();

        return DbSession.Current.CreateCriteria<ItemPrice>()
            .Add(Restrictions.Eq("Effective", effective))
            .CreateCriteria("PriceList")
            .Add(Restrictions.Eq("ListName", listName))
            .Add(Restrictions.Eq("Active", true))
            .List<ItemPrice>();

结果:结束实现自定义IUserType以将DateTime存储为字符串,并为DateTime添加Fluent Automapping约定以使用它(包含在下面):

class SQLiteDateTime : IUserType
    {
        #region IUserType Members

        public object Assemble(object cached, object owner)
        {
            return cached;
        }

        public object DeepCopy(object value)
        {
            var dt = (DateTime) value;
            return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
        }

        public object Disassemble(object value)
        {
            return String.Format("{0:yyyy'-'MM'-'dd' 'HH':'mm':'ss.fff}", value);
        }

        public new bool Equals(object x, object y)
        {
            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public bool IsMutable
        {
            get { return false; }
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        { 
            string dateString = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
            DateTime result = DateTime.ParseExact(dateString, "yyyy'-'MM'-'dd' 'HH':'mm':'ss.fff", CultureInfo.InvariantCulture.DateTimeFormat);

            return result;
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {  
            if (value == null)
            {
                NHibernateUtil.String.NullSafeSet(cmd, null, index);
                return;
            }
            value = Disassemble(value);
            NHibernateUtil.String.NullSafeSet(cmd, value, index);
        }  

        public object Replace(object original, object target, object owner)
        {
            return original;
        }

        public Type ReturnedType
        {
            get { return typeof (DateTime); }
        }

        public NHibernate.SqlTypes.SqlType[] SqlTypes
        {
            get {   
            var types = new SqlType[1];  
            types[0] = new SqlType(DbType.String);  
            return types;  
            } 
        }

        #endregion
    }

1 个答案:

答案 0 :(得分:1)

尝试为该列设置CustomType时间戳。我认为这是保存毫秒之间的区别。