1

I created the console application using Nhibernate.
I created hibernate.cfg.xml file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"
                         assembly="NHibernateDemo"
                         namespace="NHibernateDemo">
    <session-factory>
        <property name="connection.connection_string_name">default</property>
        <property name="connection.driver_class">Nhibernate.Driver.SqlClientDriver</property>
        <property name="dialect">Nhibernate.Dialect.MsSql2008Dialect</property>
        <mapping assembly ="NHibernateDemo" />
    </session-factory>
</hibernate-configuration>

and it's my code

using System;
using System.Linq;
using System.Reflection;
using HibernatingRhinos.Profiler.Appender.NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Linq;

namespace NHibernateDemo
{
internal class Program
{
    static void Main(string[] args)
    {
        NHibernateProfiler.Initialize();

        var cfg = new Configuration();
        cfg.Configure("hibernate.cfg.xml");
        var sessionFactory = cfg.BuildSessionFactory();

        int newId;

        using (var session = sessionFactory.OpenSession())
        using (var tx = session.BeginTransaction())
        {
            var newCustomer = CreateCustomer();
            Console.WriteLine("Before saving:");
            Console.WriteLine(newCustomer);
            session.Save(newCustomer);
            newId = newCustomer.Id;
            tx.Commit();
        }

        using (var session = sessionFactory.OpenSession())
        using (var tx = session.BeginTransaction())
        {
            var newCustomer = session.Load<Customer>(newId);
            Console.WriteLine("\nAfter saving:");
            Console.WriteLine(newCustomer);
            session.Save(newCustomer);
            tx.Commit();
        }

        Console.WriteLine("Enter any key to exit...");
        Console.ReadKey();
    }

    private static Customer CreateCustomer()
    {
        return new Customer
        {
            FirstName = "Jonh",
            LastName = "Doe",
            Points = 100,
            HasGoldStatus = true,
            //              MemberSince = new DateTime(2012, 1, 1),
            CreditRating = CustomerCreditRating.Neutral,
            AverageRating = 42.44454647,
            Adress = new Location()
            {
                Street = "123 Somewhere Avenue",
                City = "Nowhere",
                Province = "Alberta",
                Country = "Canada"
            }
        };
    }
}
}

but when I tried to debug my application, I got HibernateConfigException The 'assembly' attribute is not declared

enter image description here

How to fix that?

UPD if I remove attributes assembly and namespace from hibernate.cfg.xml file I get other error
MappingException was unhandled
enter image description here

UPD2 My mapping file (Build Action = Embedded Resource)

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="NHibernateDemo"
                  namespace="NHibernateDemo">
    <class name="Customer">
        <id name="Id">
            <generator class="native" />
        </id>
    <property name="FirstName"/>
    <property name="LastName"/>
    <property name="AverageRating"/>
    <property name="Points"/>
    <property name="HasGoldStatus"/>
    <property name="MemberSince" type="UtcDateTime"/>
    <property name="CreditRating" type="CustomerCreditRatingType"/>

    <component name="Adress">
        <property name="Street"/>
        <property name="City"/>
        <property name="Province"/>
        <property name="Country"/>
    </component>
</class>
</hibernate-mapping>

1 Answer 1

1

You need to add the assambly in code:

    var cfg = new Configuration();
    cfg.AddAssembly("NHibernateDemo");
    cfg.Configure("hibernate.cfg.xml");
Sign up to request clarification or add additional context in comments.

6 Comments

If I add cfg.AddAssembly("NHibernateDemo"); in my code I get the exception from UPD MappingException was unhandled. Besides, I have in my hibernate.cfg.xml file <mapping assembly ="NHibernateDemo" />
Then there is something wrong in your mapping files, what is the stacktrace? You need to add the assamblies in code.
How does nh know some of your types? Take the adress for example.
I tried to add in my hibernate.cfg.xml this code <mapping resource="Customer.hbm.xml"/>, but I get the same exception MappingException was unhandled again.
Try first to remove all properties and component from your mapping. Just to findout what gives you the mapping exception. And check if your customer is in the right namespace.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.