2013-01-08 76 views
5

我是NHibernate的新手。我正在嘗試在SQLite中使用這個ORM。 我有以下幾點: projectNHibernate.MappingException:無法編譯映射文檔

Product.cs

namespace Stock.Models.Classes 
{ 
    class Product 
    { 
     public virtual string Name {get; set;} 
     public virtual int Id { get; set; } 
     public virtual decimal Price { get; set; } 
     public virtual decimal Quantity { get; set; } 
    } 
} 

Product.hbm.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="Stock.Models" 
        namespace="Stock.Models.Classes"> 
    <class name="Product" table="products"> 
    <id name="Id"> 
     <generator class="int" /> 
    </id> 
    <property name="Name" /> 
    <property name="Price" /> 
    <property name="Quantity" /> 
    </class> 
</hibernate-mapping> 

的hibernate.cfg.xml

<?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="connection.driver_class">NHibernate.Driver.SQLite20Driver</property> 
    <property name="connection.connection_string">Data Source=nhibernate.db;Version=3</property> 
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property> 
    <property name="query.substitutions">true=1;false=0</property> 
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 
    <property name="show_sql">true</property> 
    </session-factory> 
</hibernate-configuration> 

我想在Program.cs中測試。 的Program.cs

namespace Stock 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      //Application.Run(new Form1()); 
      var cfg = new Configuration(); 
      cfg.Configure(); 
       cfg.AddAssembly(typeof(Product).Assembly); 
     } 
    } 
} 

在運行時,在該行cfg.AddAssembly(typeof(Product).Assembly);應用程式的空檔,在彈出的消息:Could not compile the mapping document: Stock.Models.Mappings.Product.hbm.xml和控制檯:

The thread 'vshost.LoadReference' (0x10a8) has exited with code 0 (0x0). 
'Stock.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\...\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Stock.exe', Symbols loaded. 
'Stock.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\...\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\NHibernate.dll' 
'Stock.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'Stock.vshost.exe' (Managed (v4.0.30319)): Loaded 'jsw3tdap' 
'Stock.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\...\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Iesi.Collections.dll' 
A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll 

如果我添加一個try/catch在的Program.cs

try 
    { 
     cfg.AddAssembly(typeof(Product).Assembly); 
    } 
catch (Exception e) { Console.WriteLine(e); } 

我得到(CON唯一):

A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll 
NHibernate.MappingException: Could not compile the mapping document: Stock.Models.Mappings.Product.hbm.xml ---> NHibernate.MappingException: persistent class Stock.Models.Classes.Product, Stock.Models not found ---> System.IO.FileNotFoundException: Could not load file or assembly 'Stock.Models' or one of its dependencies. El sistema no puede encontrar el archivo especificado. 
    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) 
    at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) 
    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) 
    at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) 
    at System.Reflection.Assembly.Load(String assemblyString) 
    at NHibernate.Util.ReflectHelper.TypeFromAssembly(AssemblyQualifiedTypeName name, Boolean throwOnError) 
    at NHibernate.Util.ReflectHelper.ClassForName(String name) 
    at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForFullNameChecked(String fullName, String errorMessage) 
    --- End of inner exception stack trace --- 
    at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForFullNameChecked(String fullName, String errorMessage) 
    at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForNameChecked(String name, Mappings mappings, String errorMessage) 
    at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindClass(IEntityMapping classMapping, PersistentClass model, IDictionary`2 inheritedMetas) 
    at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas) 
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas) 
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas) 
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema) 
    at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) 
    --- End of inner exception stack trace --- 
    at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) 
    at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) 
    at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) 
    at NHibernate.Cfg.Configuration.ProcessMappingsQueue() 
    at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) 
    at NHibernate.Cfg.Configuration.AddXmlReader(XmlReaThe thread 'vshost.RunParkingWindow' (0x1700) has exited with code 0 (0x0). 
The thread '<No Name>' (0x5d4) has exited with code 0 (0x0). 
The program '[4428] Stock.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0). 
The program '[4428] Stock.vshost.exe: Program Trace' has exited with code 0 (0x0). 
der hbmReader, String name) 
    at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) 
    at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) 
    at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly) 
    at Stock.Program.Main() in C:\Users\Cristhian\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 25 

XML文件seted爲Embedded ResourceCopy Always。爲什麼不編譯映射文檔Stock.Models.Mappings.Product.hbm.xml?它是文件格式還是庫DLL問題?

回答

9

好吧,Stock.Models程序集不存在。存在唯一的組件Stock,所以你需要相應地改變你的映射文件:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="Stock" 
        namespace="Stock.Models.Classes"> 
0

我有同樣的問題。它位於「程序集」和「命名空間」中。我做了什麼來解決這個問題,就是在你的例子中命名爲「股票」。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="Stock" 
        namespace="Stock"> 
0

儘管我的映射文件是正確的,但我仍然收到相同的錯誤。

原來在C:\inetpub\wwwroot\wss\VirtualDirectories\<port>\bin文件夾中不存在更新後的程序集。當我手工複製它們時,它工作。

我沒有解釋爲什麼他們沒有得到複製,或者即使他們應該是,我只發佈這個答案,因爲它解決了我的問題,我還沒有看到它在其他地方在線記錄。

1

您應該在hbm.xml文件中添加程序集和命名空間以查找typeof(yourModel).Assembly的使用情況並檢查fullName。 大會和班級的名字不一樣。

相關問題