7

遇到的问题

在运行时,我总是得到以下信息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. 视窗 7 专业版;
  2. Visual Studio 2010,面向 .NET 4.0;
  3. NHibernate 3.0.0.GA;
  4. SQL 服务器 2005。

问题

我已经尝试将 dialect 属性放在配置的行上,但它都不起作用。

如何解决我遇到的这个方言问题?

4

2 回答 2

10

对我来说看起来没问题......你见过这些相关的问题:

这些是容易犯的错误,可能会引发给定的异常。

于 2011-01-08T13:01:01.117 回答
3

有两件事可以解决这个问题:

不要使用这个:

Configuration c = new Configuration();

相反,使用这个:

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

确保在 hibernate.cfg.xml 文件中执行此操作:

<mapping assembly="Your assembly"/>

或者

AddAssembly(Assembly.GetCallingAssembly());

两者都做会产生问题。

于 2012-07-19T16:51:29.580 回答