nhibernate criteria - table name has double quotes

穿精又带淫゛_ 提交于 2019-12-12 06:17:20

问题


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


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/3533847/nhibernate-criteria-table-name-has-double-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!