NHibernate MappingException

时间:2017-04-02 04:49:49

标签: c# nhibernate

我一直对学习NHibernate很感兴趣,所以我搜索了一个课程,我在Pluralsight中找到了一个,但是当我按照这些例子我得到这个例外,我不知道为什么我会得到这个例外......它有点烦人,因为我搜索的地方无法找到有关异常的更多信息或NHibernate的最新指南。所以我的问题是:
a)对于例外本身,为什么会发生这种情况 b)如果您可以向我推荐一个网站或课程或任何最新的东西来学习NHibernate。

提前致谢。

我将代码留在这里:

Customer.cs:

namespace NHibernateDemo
{
    public class Customer
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    }
}

Customer.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:hibernate-mapping-2.2"
                   assembly="NHibernateDemo"
                   namespace="NHibernateDemo">
  <class name="Customer">
    <id name="Id">
      <generator class="native"/>
    </id>
    <property name="FirstName"/>
    <property name="LastName"/>
  </class>
</hibernate-mapping>

的Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;

namespace NHibernateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var cfg = new Configuration();
            cfg.DataBaseIntegration(x =>
            {
                x.ConnectionString = "Server=localhost;Database=NHibernateDemo;Integrated Security=SSPI;";
                x.Driver<SqlClientDriver>();
                x.Dialect<MsSql2008Dialect>();
            });

            // Here is where i get the MappingException. It says that it can't compile the Customer.hbm.xml file.
            cfg.AddAssembly(Assembly.GetExecutingAssembly());

            var sessionFactory = cfg.BuildSessionFactory();
            using (var session = sessionFactory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    var customers = session.CreateCriteria<Customer>()
                                           .List<Customer>();
                    foreach (var customer in customers)
                    {
                        Console.WriteLine("{0} {1}", customer.FirstName, customer.LastName);
                    }
                    tx.Commit();
                }
                Console.WriteLine("Press <ENTER> to exit...");
                Console.ReadLine();
            }
        }
    }
}

异常消息:

"Could not compile the mapping document: NHibernateDemo.Customer.hbm.xml"

堆栈跟踪:

    NHibernate.MappingException: Could not compile the mapping document: NHibernateDemo.Customer.hbm.xml ---> System.InvalidOperationException: Error en el documento XML (1, 2). ---> System.InvalidOperationException: No se esperaba <hibernate-mapping xmlns='urn:hibernate-mapping-2.2'>.
   en Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderHbmMapping.Read109_hibernatemapping()
   --- Fin del seguimiento de la pila de la excepción interna ---
   en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   en System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
   en NHibernate.Cfg.NamedXmlDocument..ctor(String name, XmlDocument document, XmlSerializer serializer)
   en NHibernate.Cfg.NamedXmlDocument..ctor(String name, XmlDocument document)
   en NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name)
   --- Fin del seguimiento de la pila de la excepción interna ---
   en NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
   en NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name)
   en NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
   en NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
   en NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
   en NHibernateDemo.Program.Main(String[] args) en d:\Sistema\Documents\Visual Studio 2015\Projects\NHibernateDemo\NHibernateDemo\Program.cs:línea 28

2 个答案:

答案 0 :(得分:1)

问题是找不到要编译的文件,所以:

尝试手动将文件复制到bin目录(DebugRelease或..)

确保您的NHibernateDemo.Customer.hbm.xml属性为Build Action : Embedded resourceCopy to Output Directory : Copy Always

如果您仍有问题,请尝试添加:cfg.AddFile("NHibernateDemo.Customer.hbm.xml")(或hbm.xml文件的路径)

如果您仍有问题,请尝试:
cfg.addFile(AssemblyLocation() + "NHibernateDemo.Customer.hbm.xml");(或hbm.xml文件的路径,其中AssemblyLocation()为:

private string AssemblyLocation()
{
    var codebase = new Uri(Assembly.GetExecutingAssembly().CodeBase);

    return Path.GetDirectoryName(codebase.LocalPath);
}

cfg.AddAssembly不是必需的,因此如果以上都不起作用,您可以考虑删除它:P

答案 1 :(得分:1)

我发现(感谢Fréderic)问题是hbm.xml文件中的一行。这条线是导致问题发生的那条线:<hibernate-mapping xmlns="urn:hibernate-mapping-2.2"。我忘记了'hibernate'字样前面的'n',因为它无法解析文件。

感谢Taki和Fréderic。