2011-01-08 68 views
6

問題遇到NHibernate的:如何解決這個 「方言」 的配置問題

在運行時,我總是得到以下NHibernate.MappingException

"Could not compile the mapping document: GI.InventoryManager.CYB.Mappings.Part.hbm.xml" 

是的,它的生成操作設置爲Embedded Resource。設置InnerException說:

"Could not find the dialect in the configuration" 

所需信息

這裏是我的配置文件命名爲hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > 
    <session-factory> 
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
    <property name="connection.connection_string"> 
     Server=(local);initial catalog=GI_IM_CYB;Integrated Security=SSPI 
    </property> 
    <property name="adonet.batch_size">10</property> 
    <property name="show_sql">false</property> 
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> 
    <property name="use_outer_join">true</property> 
    <property name="command_timeout">60</property> 
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> 
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,  NHibernate.ByteCode.Castle</property> 
    </session-factory> 
</hibernate-configuration> 

這實際上是一個複製粘貼從Configuration_Templates文件夾中,我只更改了以下信息:

Session Factory: "Removed the NHibernate.Test namespace and let the property for itself" 
Dialect: "From MsSql2000Dialect To MsSql2005Dialect" 
Connection_String: "I changed the Initial Catalog attribute to input my own database name" 
Factory Class: "From LinFu to Castle" 

這裏就是我如何使用它在我的代碼:

private void configBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { 
    Configuration c = new Configuration(); 
    c.AddAssembly(typeof(Part).Assembly); 
    lock (_sessionFactory) { 
     _sessionFactory = c.BuildSessionFactory(); 
    } 
} 

可選信息

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="GI.InventoryManager.CYB" namespace="GI.InventoryManager.CYB.Types"> 
    <class name="Part" table="Parts" lazy="true"> 
    <id name="Id" column="part_id"> 
     <generator class="native"/> 
    </id> 
    <properties name="Description"/> 
    <properties name="Number"/> 
    <properties name="InStockQty"/> 
    <properties name="Cost"/> 
    </class> 
</hibernate-mapping> 


public class Part { 
    #region Private Members 

    private string _description; 
    private string _number; 

    #endregion 
    #region Constructors 

    /// <summary> 
    /// Initializes an instance of the GI.InventoryManager.CYB.Types.Part class. 
    /// </summary> 
    public Part() { } 

    #endregion 
    #region Properties 

    /// <summary> 
    /// Gets or sets the description of this part. 
    /// </summary> 
    public virtual string Description { 
     get { 
      return _description; 
     } set { 
      if (!string.IsNullOrWhiteSpace(value)) 
       _description = value.Trim(); 
     } 
    } 

    /// <summary> 
    /// Gets the underlying datastore unique identifier. 
    /// </summary> 
    public virtual int Id { get; private set; } 

    /// <summary> 
    /// Gets or sets the user-defined number. 
    /// </summary> 
    public virtual string Number { 
     get { 
      return _number; 
     } set { 
      if (!string.IsNullOrWhiteSpace(value)) 
       _number = value.Trim(); 
     } 
    } 

    /// <summary> 
    /// Gets or sets the in-stock quantity. 
    /// </summary> 
    public virtual int InStockQty { get; set; } 

    /// <summary> 
    /// Gets or sets the cost. 
    /// </summary> 
    public virtual double? Cost { get; set; } 

    /// <summary> 
    /// Gets the inventory value for this part. 
    /// </summary> 
    /// <remarks> 
    /// <para> 
    /// This read-only property returns the product of <see cref="T:InStockQty"/> and <see cref="Cost"/>. 
    /// In case the <b>Cost</b> property does not have a value, zero is returned. 
    /// </para> 
    /// </remarks> 
    public double InventoryValue { 
     get { 
      if (Cost.HasValue) 
       return InStockQty * Cost.Value; 
      return 0.0; 
     } 
    } 

    #endregion 
    #region Methods 



    #endregion 
} 

環境

  1. 的Windows 7專業版;
  2. Visual Studio 2010,面向.NET 4.0;
  3. NHibernate 3.0.0.GA;
  4. SQL Server 2005的

問題

我已經試圖把方言財產上的配置線,它既不工作。

如何解決我的這個方言問題?

+1

下載NHibernate源代碼,附加到應用程序並嘗試在拋出時捕獲異常。 – 2011-01-08 10:47:21

+0

還沒有與NH 3一起工作 - 我有點驚訝地在您的配置文件中讀取urn:nhibernate-configuration -___ 2.2 ____。 – Marijn 2011-01-08 13:02:47

回答

3

兩件事情會解決這個問題:

不要使用此:

Configuration c = new Configuration(); 

相反,使用這樣的:

Configuration c = new Configuration().Configure(); 

確保無論你在休眠做到這一點。 cfg.xml文件:

<mapping assembly="Your assembly"/> 

AddAssembly(Assembly.GetCallingAssembly()); 

這樣做會造成問題。