我尝试使用 NHibernate ORM 制作一个项目,有一次当我认为我完成了所有工作时,它给了我一个NHibernate.MappingException: No persister for exception 我读到问题可能是我没有添加程序集在配置文件中,但我这样做了,而且它也没有修复......
如果有人有一点时间,请帮助我解决问题。
这是我用来添加新 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>
这是App.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(Object sender, EventArgs e) 在 E:\projects\tests\NHibernate\NHibernateExperiment\NHibernateExperiment\Form1.cs: 系统中 System.Windows.Forms.Control.OnClick(EventArgs e) 的第 72 行。 Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows。 Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage( Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(Form mainForm ) 在 E:\projects\tests\NHibernate\NHibernateExperiment\NHibernateExperiment\Program.cs:第 16 行中的 NHibernateExperiment.Program.Main() System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly (字符串 assemblyFile,证据 assemblySecurity,String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart()对象状态)在 System.Threading.ThreadHelper.ThreadStart()对象状态)在 System.Threading.ThreadHelper.ThreadStart()
我在 NHibernate.dll和NHibernate.ByteCode.LinFu.dll项目中添加了以下 2 个引用
非常感谢你的帮助!
谢谢你们。.hbm.xml文件的构建操作是Content。正如你建议我的那样,我将其更改为嵌入式资源并且一切正常:)