由于映射文件,NHibernate 连接失败

本文关键字:NHibernate 连接 失败 文件 于映射 映射 | 更新日期: 2023-09-27 18:36:01

我正在开发一个使用 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())

由于映射文件,NHibernate 连接失败

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

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

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