我一直对学习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"StackTrace:
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发布于 2017-04-02 16:33:36
问题是它找不到编译它的文件,所以:
尝试手动将文件复制到您的bin目录( Debug或Release或..。)
确保您的NHibernateDemo.Customer.hbm.xml属性是Build Action : Embedded resource和Copy 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
https://stackoverflow.com/questions/43164743
复制相似问题