views:

101

answers:

2

Hello,

In my quest to further my knowledge, I'm trying to get get NHibernate running.

I have the following structure to my solution

  • Core Class Library Project
  • Infrastructure Class Library Project
  • MVC Application Project
  • Test Project

In my Core project I have created the following entity:

using System;

namespace Core.Domain.Model
{
    public class Category
    {
        public virtual Guid Id { get; set; }

        public virtual string Name { get; set; }
    }
}

In my Infrastructure Project I have the following mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
         namespace="Core.Domain.Model"
         assembly="Core">

  <class name="Category" table="Categories" dynamic-update="true">
    <cache usage="read-write"/>
    <id name="Id" column="Id" type="Guid">
      <generator class="guid"/>
    </id>
    <property name="Name" length="100"/>
  </class>
</hibernate-mapping>

With the following config file:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">server=xxxx;database=xxxx;Integrated Security=true;</property>
    <property name="show_sql">true</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="cache.use_query_cache">false</property>
    <property name="adonet.batch_size">100</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <mapping assembly="Infrastructure" />
  </session-factory>
</hibernate-configuration>

In my test project, I have the following Test

 [TestMethod]
        [DeploymentItem("hibernate.cfg.xml")]
        public void CanCreateCategory()
        {
            IRepository<Category> repo = new CategoryRepository();
            Category category = new Category();
            category.Name = "ASP.NET";

            repo.Save(category);

        }

I get the following error when I try to run the test:

Test method Volunteer.Tests.CategoryTests.CanCreateCategory threw exception: NHibernate.MappingException: No persister for: Core.Domain.Model.Category.

Any help would be greatly appreciated. I do have the cfg build action set to embedded resource.

Thanks!

A: 

Hi Mike

I am guessing the problem is that your configuring your SessionFactory in your Test assembly without telling it that your mappings are in your Core assembly, something like

ISessionFactory factory = new Configuration().Configure()
    .AddAssembly(typeof(Category).Assembly) <========***
    .BuildSessionFactory();

If that doesn't help, post your configuration code.

Cheers,
Berryl

Berryl
I don't know where to add that code exactly in my Test Assembly. I'm following the tutorial found here http://forerunnerg34.wordpress.com/2009/11/03/using-asp-net-mvc-and-nhibernate-part-1/ and looking through the downloaded source files from the site, I don't see anything like you've posted.Thank
Mike
A: 

The Build Action of the XML mapping file must be set to Embedded Resource in the Infrastructure assembly. When you use the following instruction in your config file: <mapping assembly="Infrastructure" /> it will look for mappings as embedded resources in this assembly.

Darin Dimitrov
I did that and copied the same config file to my Test assembly (which is set to embedded resource too)
Mike
You don't have to embed the config file, only the mapping.
Darin Dimitrov
That was it. Can't believe I overlooked it. Thanks for your help!
Mike