Fluent NHibernate 将日期和时间字段合并为 DateTime 对象

发布于 2024-11-10 07:19:20 字数 574 浏览 2 评论 0原文

我有一个数据库表(我无法更改),它将日期和时间存储在单独的字段中,但我的类只有一个 DateTime 属性(已打开)。

DateOpened 2011-05-10 00:00:00.000
TimeOpened 1899-12-30 09:53:00.000

在 SQL 中我可以做

SELECT DateOpened + TimeOpened AS 'Opened'

How can I map this in Fluent NHibernate?我正在使用流畅的映射。

我已经尝试过

Map(x => x.Opened).Columns.Add(new string[] { "DateOpened", "TimeOpened" });

,但出现以下错误

property mapping has wrong number of columns: CBS.Tigerpaw.Data.ServiceOrder.Opened type: DateTime

I have a database table (which I cannot change) which stores the date and time in separate fields but my class only has one DateTime property (Opened).

DateOpened 2011-05-10 00:00:00.000
TimeOpened 1899-12-30 09:53:00.000

In SQL I could just do

SELECT DateOpened + TimeOpened AS 'Opened'

How can I map this in Fluent NHibernate? I'm using Fluent Mapping.

I have tried

Map(x => x.Opened).Columns.Add(new string[] { "DateOpened", "TimeOpened" });

but I get the following error

property mapping has wrong number of columns: CBS.Tigerpaw.Data.ServiceOrder.Opened type: DateTime

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城凉梦凉人心 2024-11-17 07:19:20

如果 IUsertype 是一个选项

public class DateTimeUserType : ImmutableUserType
{
    public override object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var date = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
        var time = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
        return new DateTime(date.Year, ..., time.Hours, ...);
    }

    public override void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        DateTime dt = (DateTime)(value ?? DateTime.MinValue);
        NHibernateUtil.DateTime.NullSafeSet(cmd, dt.Date, index);
        NHibernateUtil.DateTime.NullSafeSet(cmd, new DateTime(1899, 12, 30, dt.Hours, dt.Minutes, dt.Seconds), index + 1);
    }

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

    public override SqlType[] SqlTypes
    {
        get { return new[] { SqlTypeFactory.DateTime, SqlTypeFactory.DateTime }; }
    }
}



Map(x => x.Opened)
    .Columns.Add("DateOpened", "TimeOpened")
    .CustomType<DateTimeUserType>();

if IUsertype is an option

public class DateTimeUserType : ImmutableUserType
{
    public override object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var date = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
        var time = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
        return new DateTime(date.Year, ..., time.Hours, ...);
    }

    public override void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        DateTime dt = (DateTime)(value ?? DateTime.MinValue);
        NHibernateUtil.DateTime.NullSafeSet(cmd, dt.Date, index);
        NHibernateUtil.DateTime.NullSafeSet(cmd, new DateTime(1899, 12, 30, dt.Hours, dt.Minutes, dt.Seconds), index + 1);
    }

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

    public override SqlType[] SqlTypes
    {
        get { return new[] { SqlTypeFactory.DateTime, SqlTypeFactory.DateTime }; }
    }
}



Map(x => x.Opened)
    .Columns.Add("DateOpened", "TimeOpened")
    .CustomType<DateTimeUserType>();
血之狂魔 2024-11-17 07:19:20

您可以使用 .Map(...).Formula(...) & 定义 DateOpened + TimeOpened您的班级中具有私有设置器的附加属性。

you can define that DateOpened + TimeOpened by using the .Map(...).Formula(...) & additional property in your class which has a private setter.

旧梦荧光笔 2024-11-17 07:19:20

您可能会考虑独立映射两个单独的列,然后让您的类提供一个附加的(且未映射的)“Opened”属性来集成它们的值。遗憾的是,这两个映射属性仍然作为公共属性可见,因为 Fluent NHibernate 需要这样做,以便映射类中的 lambda 表达式可以获取它们。

You might consider mapping the two separate columns independently and then having your class offer an additional (and un-mapped) "Opened" property that integrates their values. Regrettably, the two mapped properties would still be visible as public properties, since Fluent NHibernate requires this so that the lambda expressions in the mapping class can get at them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文