3

我正在尝试为同一个类/表映射父/子之间的双向多对一关系。这是映射:

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

当我尝试用孩子列表保存父母时,出现以下错误

System.InvalidCastException:对象必须实现 IConvertible。

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

References(x => x.Parent).Column("ParentID");

然后我可以保存,但如果我取一个孩子,则父母为空。

任何想法如何解决这个问题?


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

System.InvalidCastException:对象必须实现 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();
4

1 回答 1

0

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

于 2013-05-09T13:10:15.883 回答