我有几个测试我正在顺序运行(不使用ms测试)。我遇到一个问题,当我尝试使用NHibernate删除对象时,当我尝试运行任何类型的查询时(除了使用NHibernate添加的例外),它会强制下一个测试失败。我已经能够通过在删除语句后使用Unbind和Bind解决此问题,但这需要很长时间。我想知道是否还有其他方法可以解决这个问题。
一个例子:
[TestMethod]
public void MyTest() {
QTestData testData = new QTestData();
testData.MyProperty = "CookieMonsterPanda";
QNHibernateHelper.Add(testData);
//Still working at this point
testData.MyProperty = "FooBar";
QNHibernateHelper.Update(testData);
//Still working at this point
Assert.AreEqual(testData, RQTestData.GetTestDataByMyProperty("FooBar"));
QNHibernateHelper.Delete(testOrderLine);
//If I were to run this test again or any other test without these
//next two lines I would get an exception
QNHibernateHelper.UnbindSession();
QNHibernateHelper.BindSession();
}
示例异常和NHibernate堆栈跟踪:
Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.Exceptions.GenericADOException: could not delete: [Quorra.Entities.QTestData#12915489][SQL: DELETE FROM test_data WHERE testdatanumber = ?] ---> MySql.Data.MySqlClient.MySqlException: Cannot delete or update a parent row: a foreign key constraint fails (`stuff_20130619`.`test_data`, CONSTRAINT `FK_test_data__test_data` FOREIGN KEY (`origtestdata`) REFERENCES `test_data` (`stuff`) ON DELETE NO ACTION ON UPDATE NO ACTION)
堆栈追踪:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState)
--- End of inner exception stack trace ---
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState)
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityDeleteAction.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.DefaultAutoFlushEventListener.OnAutoFlush(AutoFlushEvent event)
at NHibernate.Impl.SessionImpl.AutoFlushIfRequired(ISet`1 querySpaces)
at NHibernate.Impl.SessionImpl.List(String query, QueryParameters queryParameters, IList results)
at NHibernate.Impl.SessionImpl.List[T](String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List[T]()
编辑1:
它打破了同一类型对象的foriegn键(它是同一类的父类)。问题是在这种情况下,我得到的其他错误具有相同的性质,这些键被设置为null。它们是由nhibernate动态创建的,将它们添加到db中。什么让我感到震惊,但是当我添加一个绑定/解除绑定时,它能够解决这个问题。