开发者

Cannot excute query in NHibernate

开发者 https://www.devze.com 2023-03-17 00:56 出处:网络
I try to use this tutorial: Hello NHibernate This is my config NHibernate file: <?xml version=\"1.0\"?>

I try to use this tutorial: Hello NHibernate

This is my 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>
  &开发者_开发技巧lt;/hibernate-configuration></configuration>

This is my 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 is 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 as u");
                    IList<user> lst = query.List<user>();
                    foreach (var user in lst)
                    {
                        Console.WriteLine(user.Name);
                    }
                }

It's always show error:

could not execute query [ select user0_.Id as Id0_, user0_.Name as Name0_ from user user0_ ] [SQL: select user0_.Id as Id0_, user0_.Name as Name0_ from user user0_]

I tried to insert, update but it's also show could not insert, update. Where is my problem?

Is there my mapping file? Please give me advice! Thanks!


Two things:

(1) In your mapping file, you should be enclosing the User table in square brackets, e.g. [User]. User is a reserved keyword in SQL Server (and most other SQL systems).

<class name="user" table="[user]">

(2) Whenever you get an exception in NHibernate, go into debug mode and look at the inner exceptions. Those usually provide hints as to what the problem is.


If you use this code NHibernate will generate database schema and database script preventing case-sensitive or other errors.

 var conf = new NHibernate.Cfg.Configuration();
        conf.Configure();


        var export = new SchemaExport(conf);
        export.SetOutputFile(@"DatabaseScript.sql");
        export.Drop(true, true);
        export.Create(true, true);

I think also that "user" can be a reserved word ! Try:

IQuery query = session.CreateQuery("from [user] as u");


Make sure that the table and column names in your mapping exactly match those defined in the DB schema. Case-sensitivity might be important as well depending on your SQL server configuration.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号