1

我有一个实现 NHibernate 的项目,该项目在我的带有 Visual Studio 2010 的机器上运行良好。当我将该项目导入 Visual Studio 2012 时,我遇到了 NHibernate.MappingException

.hbm.xml 设置为嵌入式资源,并且复制到输出目录设置为所有 .hbm.xml 文件的始终复制。

该项目在 VS 2010 下运行,所以我不确定它如何可能是映射问题。


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="connection.connection_string">Data Source=FirstSample.sdf</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateExample" namespace="HibernateExample.Domain" >
  <class name="Grains" table="Products">
    <id name="Id" type="integer">
      <generator class="identity"/>
    </id>
    <property name="Name" type="string"/>
    <property name="Category" type="string"/>
    <property name="Discontinued" />
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateExample" namespace="HibernateExample.Domain" >
  <class name="Dairy" table="Products">
    <id name="Id" type="integer">
      <generator class="identity"/>
    </id>
    <property name="Name" type="string"/>
    <property name="Category" type="string"/>
    <property name="Discontinued" />
  </class>
</hibernate-mapping>

应用程序配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
      <property name="connection.driver_class"> NHibernate.Driver.SqlServerCeDriver</property>
        <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
      <property name="connection.connection_string">Data Source=FirstSample.sdf;</property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>


  <connectionStrings>
        <add name="testconnectionstring"
            connectionString="Data Source=|DataDirectory|\FirstSample.sdf;Integrated Security=True"
            providerName="Microsoft.SqlServerCe.Client.3.5" />
    </connectionStrings>

  <runtime>
    <assemblyBinding xmlns="urnchemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845DCD8080CC91" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-9.0.242.0" newVersion="3.5.0.0"/>
      </dependentAssembly>
    </assemblyBinding>

  </runtime>
</configuration>

A first chance exception of type 'System.TypeLoadException' occurred in mscorlib.dll

A first chance exception of type 'System.TypeLoadException' occurred in NHibernate.dll

A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll

A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll

A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll

A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll

Error: NHibernate.MappingException: Could not compile the mapping document: HibernateExample.Domain.Dairy.hbm.xml ---> NHibernate.MappingException: persistent class HibernateExample.Domain.Dairy, HibernateExample not found ---> System.TypeLoadException: Could not load type 'HibernateExample.Domain.Dairy' from assembly 'HibernateExample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)

at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)

at NHibernate.Util.ReflectHelper.TypeFromAssembly(AssemblyQualifiedTypeName name, Boolean throwOnError) in p:\nhibernate-core\src\NHibernate\Util\ReflectHelper.cs:line 308

at NHibernate.Util.ReflectHelper.ClassForName(String name) in p:\nhibernate-core\src\NHibernate\Util\ReflectHelper.cs:line 181

at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForFullNameChecked(String fullName, String errorMessage) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\Binder.cs:line 99

--- End of inner exception stack trace ---

at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForFullNameChecked(String fullName, String errorMessage) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\Binder.cs:line 105

at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForNameChecked(String name, Mappings mappings, String errorMessage) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\Binder.cs:line 117

at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindClass(IEntityMapping classMapping, PersistentClass model, IDictionary`2 inheritedMetas) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\ClassBinder.cs:line 32

at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\RootClassBinder.cs:line 21

at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:line 84

at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:line 42

at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema) in p:\nhibernate-core\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:line 31

at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 523

--- End of inner exception stack trace ---

at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 342

at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 530

at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 500

at NHibernate.Cfg.Configuration.ProcessMappingsQueue() in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1865

at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1857

at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1850

at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 652

at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 690

at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly) in p:\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 769

at HibernateExample.NHibernateTest.NHibernateHelper.OpenSession() in c:\Users\Fabian\Documents\Visual Studio 2010\Projects\NHibernate\HibernateExample\NHibernateTest\NHibernateHelper.cs:line 31

Error: Could not compile the mapping document: HibernateExample.Domain.Dairy.hbm.xml

A first chance exception of type 'System.NullReferenceException' occurred in HibernateExample.exe
4

1 回答 1

2

包含所有需要信息的内部异常在这里:

错误:NHibernate.MappingException:无法编译映射文档:HibernateExample.Domain.Dairy.hbm.xml ---> NHibernate.MappingException:持久类 HibernateExample.Domain.Dairy,未找到 HibernateExample ---> System.TypeLoadException:可以不从程序集“HibernateExample,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”加载类型“HibernateExample.Domain.Dairy”。

在工厂配置期间,NHibernate 读取所有嵌入的资源,包括第三个(从您问题的顶部开始):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
  assembly="HibernateExample" 
  namespace="HibernateExample.Domain" >
  <class name="Dairy" table="Products">

所以,这个问题很清楚。没有HibernateExample.Domain.Dairy可用的类(未部署、拼写错误或名称空间不正确)。好消息或消息是您的映射已找到并已解析...

尝试确保应用程序可以访问映射的类

于 2013-01-29T05:10:55.280 回答