2

我有一个具有三个属性的简单对象。这链接到一个包含三列的表,其中两列是主键(一个是日期时间,另一个是日期时间)。

每当我尝试查询 nhibernate 时,都会收到以下错误消息:

无法解析属性:invdate of:Models.Invoice

我在设置映射时错过了一些简单的事情,还是我的查询有误?

对象代码:

public class Invoice
{
    public Invoice() { }

    #region Properties

    public virtual int Acct_Link { get; private set; }
    public virtual DateTime Added { get; private set; }
    public virtual DateTime InvDate { get; private set; }

    public static Invoice GetInvoiceByFluent( int accountId, DateTime invoiceDate )
    {
        Invoice invoice;

        using ( ISession session = FluentNHibernateHelper.OpenSession() )
        {
            invoice = session
                .CreateCriteria<Invoice>()
                .SetMaxResults(1)
                .Add( Expression.Eq( "Acct_Link", accountId ) )
                .Add( Expression.Eq( "invdate", invoiceDate ) )
                .UniqueResult<Invoice>();
        }

        return invoice;
    }

    #region Nhibernate overrides

    public override bool Equals( object obj )
    {
        if ( obj == null )
            return false;

        Invoice i = obj as Invoice;

        return this.GetHashCode() == i.GetHashCode();
    }

    public override int GetHashCode()
    {
        return this.Acct_Link.GetHashCode() ^ this.InvDate.GetHashCode();
    }

    #endregion
}

映射代码:

    public class InvoiceMap: ClassMap<Invoice>
{
    public InvoiceMap()
    {
        Table( "Invoice" );

        CompositeId()
            .KeyProperty( i => i.Acct_Link, "Acct_Link" )
            .KeyProperty( i => i.InvDate, "InvDate" );

        Map( i => i.Added );
    }
}

如果我在查询中去掉对 invdate 的引用,那么这很好。

提前致谢。

4

1 回答 1

1

该属性被称为InvDate而不是invdate。尝试这个:

.Add( Expression.Eq( "InvDate", invoiceDate ) )
于 2010-02-24T15:17:03.180 回答