0

我使用 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 The 'assembly' attribute is not declared

在此处输入图像描述

如何解决?

如果我删除属性assemblynamespace从 hibernate.cfg.xml 文件中删除属性,则UPD我得到其他错误
MappingException is unhandled
在此处输入图像描述

UPD2我的映射文件(构建操作 = 嵌入式资源)

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

1 回答 1

1

您需要在代码中添加组件:

    var cfg = new Configuration();
    cfg.AddAssembly("NHibernateDemo");
    cfg.Configure("hibernate.cfg.xml");
于 2013-07-30T08:39:28.383 回答