流利的Nhibernate,复合键,无法解析属性:错误消息

时间:2010-02-24 14:59:27

标签: c# database nhibernate fluent-nhibernate data-access

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

每当我尝试查询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的引用,那么这样可以正常工作。

提前致谢。

1 个答案:

答案 0 :(得分:1)

该属性名为InvDate,而不是invdate。试试这个:

.Add( Expression.Eq( "InvDate", invoiceDate ) )