【问题标题】:nHibernate with SQLite format datenHibernate 与 SQLite 格式日期
【发布时间】:2012-10-20 13:37:14
【问题描述】:

我在 SQLite 数据库中使用 nHibernate + FluentNHibernate。 所有日期都以文本格式存储为 'YYYY-MM-DD HH:MM:SS'

如何指示 nHibernate(或 sqlite 驱动程序???)截断时间部分并为某些字段以 'YYYY-MM-DD' 格式存储日期(并为其他字段存储完整日期)?

或者我如何指示将所有日期存储为整数?

我担心空间使用情况,因为日期在数据库中占用了大量空间,而且日期上也有索引,所以我需要让它们占用尽可能少的空间。

【问题讨论】:

    标签: c# sqlite nhibernate


    【解决方案1】:

    如果您手动将DateTime 转换为String,我看不出用YYYY-MM-DD 替换字符串格式有什么问题。

    如果您让 NHibernate 执行此转换,那么您可以像这样创建自定义 NHibernate UserType

    public abstract class DateTimeAsStringType : IUserType
    {
        public object Assemble(object cached, object owner)
        {
            return cached;
        }
    
        public object DeepCopy(object value)
        {
            return value;
        }
    
        public object Disassemble(object value)
        {
            return value;
        }
    
        public bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y))
                return true;
    
            if (x == null && y == null)
                return false;
    
            return x.Equals(y);
        }
    
        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }
    
        public bool IsMutable
        {
            get { return false; }
        }
    
        public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
        {
            var serialized = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
    
            if (string.IsNullOrEmpty(serialized))
                return null;
    
            return Deserialize(serialized);
        }
    
        protected abstract DateTime Deserialize(string value);
        protected abstract string Serialize(DateTime value);
    
        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
        {
            if (value == null)
                NHibernateUtil.String.NullSafeSet(cmd, DBNull.Value, index);
            else
                NHibernateUtil.String.NullSafeSet(cmd, Serialize((DateTime)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 { return new[] { NHibernateUtil.String.SqlType }; }
        }
    }
    
    public class TruncatedDateTimeAsStringType : DateTimeAsStringType
    {
        private const string Format = "yyyy-MM-dd";
    
        protected override string Serialize(DateTime value)
        {
            return value.ToString(Format, CultureInfo.InvariantCulture);
        }
    
        protected override DateTime Deserialize(string value)
        {
            return DateTime.ParseExact(value, Format, CultureInfo.InvariantCulture, DateTimeStyles.None);
        }
    }
    
    public class FullDateTimeAsStringType : DateTimeAsStringType
    {
        private const string Format = "yyyy-MM-dd hh:mm:ss";
    
        protected override string Serialize(DateTime value)
        {
            return value.ToString(Format, CultureInfo.InvariantCulture);
        }
    
        protected override DateTime Deserialize(string value)
        {
            return DateTime.ParseExact(value, Format, CultureInfo.InvariantCulture, DateTimeStyles.None);
        }
    }
    

    并使用TruncatedDateTimeAsStringTypeFullDateTimeAsStringType 映射您的DateTime 属性:

    <property name="TruncatedDateTime" type="Your.Namespance.TruncatedDateTimeAsStringType, Your.Assembly" />
    <property name="NotTruncatedDateTime" type="Your.Namespance.FullDateTimeAsStringType, Your.Assembly" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 2021-08-06
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多