Cannot excute query in NHibernate -
i try use tutorial: hello nhibernate
this config nhibernate file:
<?xml version="1.0"?> <configuration> <configsections> <section name="hibernate-configuration" requirepermission="false" type="nhibernate.cfg.configurationsectionhandler, nhibernate"/> </configsections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <reflection-optimizer use="false"/> <session-factory> <property name="connection.provider">nhibernate.connection.driverconnectionprovider</property> <property name="dialect">nhibernate.dialect.mssql2008dialect</property> <property name="connection.driver_class">nhibernate.driver.sqlclientdriver</property> <property name="connection.connection_string">data source=.\sqlexpress; initial catalog=dbtest; trusted_connection=true;</property> <property name="proxyfactory.factory_class">nhibernate.bytecode.castle.proxyfactoryfactory, nhibernate.bytecode.castle</property> <property name="show_sql">true</property> <mapping resource="testnhibernate.user.hbm.xml" assembly="testnhibernate"/> </session-factory> </hibernate-configuration></configuration>
this mapping file:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="testnhibernate" namespace="testnhibernate"> <class name="user" table="user"> <id name="id"> <generator class="identity"/> </id> <property name="name" /> </class> </hibernate-mapping>
class user:
class user { public virtual int id { get;set; } public virtual string name { get; set; } }
and here query code:
configuration config = new configuration(); config.addassembly(typeof(user).assembly); isessionfactory sessionfactory = config.buildsessionfactory(); using (var session = sessionfactory.opensession()) { iquery query = session.createquery("from user u"); ilist<user> lst = query.list<user>(); foreach (var user in lst) { console.writeline(user.name); } }
it's show error:
could not execute query [ select user0_.id id0_, user0_.name name0_ user user0_ ] [sql: select user0_.id id0_, user0_.name name0_ user user0_]
i tried insert, update it's show not insert, update. problem?
is there mapping file? please give me advice! thanks!
two things:
(1) in mapping file, should enclosing user table in square brackets, e.g. [user]. user reserved keyword in sql server (and other sql systems).
<class name="user" table="[user]">
(2) whenever exception in nhibernate, go debug mode , @ inner exceptions. provide hints problem is.
Comments
Post a Comment