Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NHibernate not returning any data?

I am having some problem configuring NHibernate to retrieve data in my MVC 4 application.

To keep things simple I have configured all code in the Index method.

Here is code for my Category controller :

Category Controller code

and here is my configuration in web.config :

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="NHibernate.Test">
      <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
      </property>
      <property name="connection.connection_string">
        Data Source=.;Initial Catalog=UsingNH;uid=myuid;Password=mypwd
      </property>
      <property name="adonet.batch_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="use_outer_join">true</property>
      <property name="command_timeout">60</property>
      <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
      <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
      </property>
    </session-factory>
  </hibernate-configuration>

Mapping file for Category is

<?xml version="1.0" encoding="utf-8" ?> 

<hibernate-mapping xmlns="nhibernate-mapping-2.2" namespace="UsingNHibernate.Models" assembly="UsingNHibernate">
  <class name="Category" table="Categories" lazy="false">

    <id name="Id" columnId="Id" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="Name">
      <column name="Name" data-type="varchar(50)" not-null="true" />
    </property>

  </class>
</hibernate-mapping>

and the Category table schema is

CREATE TABLE [dbo].[Categories](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,
 CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)
) ON [PRIMARY]

The problem is that the program compiles and runs well but does not return any category.

The code

var lst = (List<Category>)criterion.List<Category>();

returns 0 items (verified in debugger).

Is there any problem in my configuration or Mapping files?

Comment if additional info is required.

Thanks.

like image 361
Dinesh Avatar asked Oct 20 '13 11:10

Dinesh


2 Answers

If no mappings are defined, nhibernate will simply fail silently and return an empty list if you query a list of entities.

I guess you do not copy over the mapping files to your bin directory. Mark the mapping files to be copied (via properties).

like image 91
MichaC Avatar answered Sep 28 '22 13:09

MichaC


I changed the Build Action Property of the hbm mapping file to Embedded Resource, then I can get the data.

enter image description here

(It's same way as the OP's comment, but I post it as an answer to be easier noticed.)

like image 31
yu yang Jian Avatar answered Sep 28 '22 14:09

yu yang Jian