我正在使用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())
答案 0 :(得分:2)
上述代码段中的class Format
似乎是从您的真实代码中复制/粘贴的。如果是这种情况,则问题是public int? Quantite
,而不是虚拟。像这样改变你的实体:
// virtual is a key to success
public virtual int? Quantite
{
get {...
期望你确实有其他实体的映射(Produits,...)这应解决问题