【问题标题】:NHibernate: storing / retrieving dates in / from SqliteNHibernate:在/从 Sqlite 中存储/检索日期
【发布时间】:2019-07-22 06:19:45
【问题描述】:

我有这个简单的对象

public class SomeObject
{
    public virtual DateTime TimeStamp { get; }
}

我想知道如何使用 nhibernate 映射 DateTime 属性 sqlite,

试过这样的

 mapping.Map(Reveal.Member<Something>("TimeStamp"))
            .Default(DateTime.UtcNow.Second.ToString())
            .Generated.Insert()
            .Not.Nullable();

尝试插入时

            session.Save(new SomeObject());
            tran.Commit();

我明白了

输入字符串“29”的格式不正确。 InvalidCastException:从“Int32”到“DateTime”的无效转换。

【问题讨论】:

  • 你为什么用“int”作为CustomSqlType

标签: c# sqlite nhibernate fluent-nhibernate


【解决方案1】:
mapping.Map(Reveal.Member<Something>("TimeStamp"))
            .CustomType<DateTime>()
            .CustomSqlType("datetime")
            .Default(DateTime.UtcNow.Second.ToString())
            .Generated.Insert()
            .Not.Nullable();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2014-01-18
    • 1970-01-01
    相关资源
    最近更新 更多