0

我正在开发一个使用 NHibernate 和 MVVM 的应用程序。我正在尝试向我的项目添加一个新的映射文件,但现在应用程序无法打开与 NHibernate 的连接(之前一切都运行良好)。

请注意,我目前没有使用与新映射文件相关的服务

Mysql 表代码(新映射文件的)

CREATE TABLE IF NOT EXISTS Formats
(idFormat INT AUTO_INCREMENT PRIMARY KEY
,idProduit  INT
,idUnite INT
,quantite INT
);

项目中的代码类:

public class Format : ObservableObject
{
    #region Membres privées
    private int? _idFormat = null;
    private Produit _produit;
    private Unite _unite;
    private int? _quantite;
    #endregion

    #region Propriétés

    /// <summary>
    /// Sets and gets the IdFormat property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public virtual int? IdFormat
    {
        get
        {
            return _idFormat;
        }

        set
        {
            if (_idFormat == value)
            {
                return;
            }

            RaisePropertyChanging();
            _idFormat = value;
            RaisePropertyChanged();
        }
    }

    /// <summary>
    /// Sets and gets the Produit property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public virtual Produit Produit
    {
        get
        {
            return _produit;
        }

        set
        {
            if (_produit == value)
            {
                return;
            }

            RaisePropertyChanging();
            _produit = value;
            RaisePropertyChanged();
        }
    }

    /// <summary>
    /// Sets and gets the Unite property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public virtual Unite Unite
    {
        get
        {
            return _unite;
        }

        set
        {
            if (_unite == value)
            {
                return;
            }

            RaisePropertyChanging();
            _unite = value;
            RaisePropertyChanged();
        }
    }

    /// <summary>
    /// Sets and gets the Quantite property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public int? Quantite
    {
        get
        {
            return _quantite;
        }

        set
        {
            if (_quantite == value)
            {
                return;
            }

            RaisePropertyChanging();
            _quantite = value;
            RaisePropertyChanged();
        }
    }
    #endregion
}

服务 :

public interface IFormatService
{
    IList<Format> RetrieveAll();
}

NHibernate 服务:

public class NHibernateFormatService : IFormatService
{
    public IList<Format> RetrieveAll()
    {
        using (var session = NHibernateConnection.OpenSession())
        {
            return session.Query<Format>().ToList();
        }
    }
}

映射(嵌入式资源):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping assembly="Econo.Bouffe"
                   namespace="Econo.Bouffe.Model"
                   xmlns="urn:nhibernate-mapping-2.2">
  <class name="Format" table="Formats">
    <id name="IdFormat">
      <column name="idFormat" not-null="true" sql-type="INTEGER" />
      <generator class="identity" />
    </id>
    <many-to-one name="Produits" class="Produit" lazy="false">
      <column name="idProduit" not-null="true" sql-type="INTERGER" />
    </many-to-one>
    <many-to-one name="Unites" class="Unite" lazy="false">
      <column name="idUnite" not-null="true" sql-type="INTERGER" />
    </many-to-one>
    <property name="Quantite">
      <column name="quantite" not-null="true" sql-type="INTERGER" />
    </property>
  </class>
</hibernate-mapping>

当我删除 .hbm.xml 映射文件时,一切正常,但是当我添加它时,我收到了这个错误:

An exception of type 'System.TypeInitializationException' occurred in Econo.Bouffe.exe but was not handled in user code

Additional information: The type initializer for 'Econo.Bouffe.Helpers.NHibernate.NHibernateConnection' threw an exception.

If there is a handler for this exception, the program may be safely continued.

在另一个 NHibernate 服务的这行代码处:

using (var session = NHibernateConnection.OpenSession())
4

1 回答 1

2

class Format在上面的代码段中似​​乎是从您的真实代码中复制/粘贴的。如果是这种情况,则问题出在public int? Quantite,它不是virtual。像这样更改您的实体:

// virtual is a key to success
public virtual int? Quantite
{
    get {...

期望您确实有其他实体(产品,...)的映射,这应该可以解决问题

于 2013-10-18T02:58:58.067 回答