HibernateConfigException解析配置时发生异常

时间:2013-07-30 08:26:02

标签: nhibernate

我使用Nhibernate创建了控制台应用程序 我创建了hibernate.cfg.xml文件

<?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>

这是我的代码

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"
            }
        };
    }
}
}

但是当我尝试调试我的应用程序时,我得到了HibernateConfigException 未声明'assembly'属性

enter image description here

如何解决这个问题?

UPD 如果我从hibernate.cfg.xml文件中删除属性assemblynamespace,我会收到其他错误
MappingException未处理
enter image description here

UPD2 我的映射文件(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 个答案:

答案 0 :(得分:1)

您需要在代码中添加程序集:

    var cfg = new Configuration();
    cfg.AddAssembly("NHibernateDemo");
    cfg.Configure("hibernate.cfg.xml");