views:

27

answers:

1

Hello, I am using Fluent NHibernate in my application. I have a criteria query that looks like this -

var query = DetachedCriteria
                .For<table2>()
                .SetProjection(Projections.Distinct(Projections.Property("id")))
                //.Add(Restrictions.Between("date_field", startDate, endDate))
                .Add(Restrictions.Eq("id", 204010));

            Add(Subqueries.In("id", query));

This errors out with the error -

NHibernate.ADOException was unhandled
Message=could not execute query

I looked at the query and tried to run it, but it also errored out. I then noticed that in the subquery, the table name for table2 is in quotes. I removed these quotes and the query ran fine. Does anyone know how I can get rid of the quotes in my criteria?

thanks for any thoughts

A: 

You need a table name convention. Something like:

public class TableNameConvention : IClassConvention, IClassConventionAcceptance
{
    public void Apply(IClassInstance instance)
    {
        instance.Table("`" + Inflector.Underscore(instance.EntityType.Name) + "´");
    }

    public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
    {
        criteria.Expect(x => x.TableName, Is.Not.Set);
    }
}

Or using old school xml:

 <class xmlns="urn:nhibernate-mapping-2.2" name="Address" table="`address´">
      <id name="Id">
      <column name="address_id" />
      <generator class="identity" />
    </id>
 </class>
mhenrixon