nhibernate criteria - table name has double quotes
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 i开发者_StackOverflow中文版n 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
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>
I found the answer - I had my entities set up to have capital first letters, but in the database they don't have caps. The quotes in the table name make the RDBMS look at the table names with case sensitivity. So I changed my entities to be all lower case, and the query works. Thanks
精彩评论