当前位置: 动力学知识库 > 问答 > 编程问答 >

Problem using VerifyTheMappings in Fluent NHibernate

问题描述:

I'm using Fluent NHibernate to map classes to a database and I'm using PersistenceSpecification.VerifyTheMappings() to verify the mappings in my unit tests.

If ClassA has a property of type ClassB and I want to verify the mapping, I first create an instance of ClassB then I try to use it with PersistenceSpecification like this:

ClassB classB = new ClassB();

new PersistenceSpecification<ClassA>(session)

.CheckProperty(x => x.ClassB, classB)

.VerifyTheMappings();

When I run the test in NUnit, the test fails with the following error:

System.ApplicationException : For property 'ClassB' expected 'MyNamespace.ClassB' of type 'MyNamespace.ClassB' but got 'ClassBProxyf24bc4...' of type 'MyNamespace.ClassB'

I also tried using "CheckReference" instead of "CheckProperty", but I got the same results. Creating the ClassB instance inline within CheckProperty() also didn't make a difference - not that I expected it to...

I've come across code examples on the web that imply that this should work. What am I missing here?

网友答案:

If ClassB is a mapped entity you should use CheckReference rather than CheckProperty.

However, the problem is that you have to help Fluent NHibernate decide if the objects are equal. You can either pass in an IEqualityComparer into the PersistenceSpecification or have your entities override the Equals method.

In the comparer / equals override you would probably want to do something like comparing the types and the primary key values.

There is a section at the Fluent NHibernate wiki about using PersistenceSpecification to test references, which includes a sample implementation of IEqualityComparer.

分享给朋友:
您可能感兴趣的文章:
随机阅读: