我尝试使用NHibernate ORM制作一个项目,并且在我认为所有完成的时候,它给了我一个NHibernate.MappingException:没有留存为例外 我看这个问题可能是因为我没有加入配置文件中的程序集,但我这样做,而且它不是固定的......NHibernate.MappingException:没有persister:NHibernateExperiment.Player - 我找不到解决方案
如果有人有一个小时,请帮我解决问题。
这里是我打电话的代码添加一个新的Player对象
private void btnInsert_Click(object sender, EventArgs e)
{
Player playerData = new Player();
SetPlayerInfo(playerData);
using (ISession session = SessionFactory.OpenSession)
{
using (ITransaction transaction = session.BeginTransaction())
{
try
{
session.Save(playerData); // here it spits
transaction.Commit();
GetPlayerInfo();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
}
}
}
private void GetPlayerInfo()
{
using (ISession session = SessionFactory.OpenSession)
{
IQuery query = session.CreateQuery("FROM Player");
IList<Player> pInfos = query.List<Player>();
dgvDisplay.DataSource = pInfos;
}
}
private void SetPlayerInfo(Player playerData)
{
playerData.PlayerName = tbxName.Text;
playerData.PlayerAge = Convert.ToInt32(tbxAge.Text);
playerData.DOJ = Convert.ToDateTime(dtpDOJ.Text);
playerData.BelongsTo = cmbBelongsTo.SelectedItem.ToString();
}
这里映射Player.hbm.xml代码
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="NHibernateExperiment.Player, NHibernateExperiment" lazy="true">
<id name="PlayerId">
<generator class="native"/>
</id>
<property name="PlayerName" column ="PlayerName"/>
<property name="PlayerAge" column ="PlayerAge"/>
<property name="DOJ" column="DOJ"/>
<property name="BelongsTo" column="BelongsTo"/>
</class>
</hibernate-mapping>
这里是应用。 config代码
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=GRITCAN;database=testDB;Integrated Security=SSPI;</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="show_sql">true</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>
</configuration>
这里是栈跟踪
在NHibernateExperiment.Form1.btnInsert_Click(对象发件人,EventArgs e)如E:\项目\测试\ NHibernate的\ NHibernateExperiment \ NHibernateExperiment \ Form1.cs中:在系统管线72 。 Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control .WmMouseUp(Message & m,MouseButtons button,Int32 clicks) at System.Windows.Forms.Control.WndProc(Message & m) at System.Windows.Forms.ButtonBase.WndProc(消息&米) 在System.Windows.Forms.Button.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM) 在System.Windows.Forms.UnsafeNativeMethods。 DispatchMessageW(MSG & msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)at System.Windows.Forms.Applicat ion.ThreadContext.RunMessageLoopInner(的Int32原因,ApplicationContext的上下文) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32原因,ApplicationContext的上下文) 在System.Windows.Forms.Application.Run(表格的MainForm) 在NHibernateExperiment .Program.Main()在E:\ projects \ tests \ NHibernate \ NHibernateExperiment \ NHibernateExperiment \ Program.cs中:第16行在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args) at System.AppDomain.ExecuteAssembly(String (System.ThreadingHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上的assemblyFile,Evidence assemblySecurity,String [] args) ,对象状态,布尔ean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state) at System.Threading.ThreadHelper。的ThreadStart()
我已经添加了以下2个引用该项目 NHibernate.dll和NHibernate.ByteCode.LinFu.dll
非常感谢您的帮助!
谢谢你BOYS。 构建动作对于.hbm.xml文件是内容。 正如你建议我,我把它改成嵌入式资源和:)
请务必改变*生成操作*为'.hbm.xml'文件*嵌入的资源*。 –
谢谢。 ** .hbm.xml **文件的** Build Action **是** Content **。我将其更改为** Embedded Resource **,并且所有工作正常:) – meorfi