流利的NHibernate双向多对一可逆异常

本文关键字:多对一 异常 NHibernate | 更新日期: 2023-09-27 18:02:25

我试图为同一个类/表映射父/子之间的双向多对一关系。下面是映射:

References(x => x.Parent).Column("ParentID");
HasMany(x => x.Children).KeyColumn("ParentID").Inverse().Cascade.All();

当我尝试用子列表保存父节点时,我得到以下错误

系统。InvalidCastException: Object必须实现IConvertible.

如果父/子是两个不同的类/表,则上面的映射有效。我还尝试通过删除:

来使映射单向
References(x => x.Parent).Column("ParentID");

然后我可以保存,但是如果我取一个子节点父节点是空的

有什么办法解决这个问题吗?


Fit.Server.Persistence.Test.Repositories.SagOpgave.SagOpgavePersistenceTest。Opgave_gets_references抛出异常:

系统。InvalidCastException: Object必须实现IConvertible.

at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at Pervasive.Data.SqlClient.PsqlParameter.a(Type A_0)
at Pervasive.Data.SqlClient.q.a(PsqlParameter A_0, k A_1, a3 A_2, Int32 A_3)
at Pervasive.Data.SqlClient.q..ctor(PsqlParameter A_0, k A_1, Int32 A_2, Int32 A_3, Int32 A_4)
at Pervasive.Data.SqlClient.PsqlParameterCollection.a(k A_0, Int32 A_1, Int32 A_2, Int32 A_3)
at Pervasive.Data.SqlClient.PsqlCommand.a(Boolean A_0, CommandBehavior A_1, Boolean A_2)
at Pervasive.Data.SqlClient.PsqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityInsertAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
at NHibernate.Engine.ActionQueue.ExecuteActions()
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()

找到一个目前为止有效的解决方案。

References(x => x.Parent).Column("ParentID").Not.Insert();
HasMany(x => x.Children).KeyColumn("ParentID").Cascade.All();

流利的NHibernate双向多对一可逆异常

我试着理解代码和错误。基于此,我可以看到您传递了一些无效数据类型或与列数据类型不匹配的数据。保存前请检查所有列的值