Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type XYZ

1,655 views
Skip to first unread message

Devin Michael

unread,
Mar 9, 2015, 12:26:09 PM3/9/15
to nhu...@googlegroups.com
I have just started using NHibernate Envers and I'm running into an issue when saving certain objects when a bag collection associated with the object has been altered.  The problem is visible within the attached image.

I'm using NHibernate v4.0.0.4000 and Envers version 2.0 GA.  I found a bug report related to prior versions of Envers https://nhibernate.jira.com/browse/NHE-136?jql=project%20%3D%20NHE but that bug was reportedly fixed in version 2.0.

If anyone has any insights on what might be causing this issue I'd greatly appreciate it.

Thank you,
Devin
Persist Collection Exception.png

Roger Kratz

unread,
Mar 10, 2015, 5:17:55 AM3/10/15
to nhu...@googlegroups.com

Don’t know I’m afraid.

Please try to isolate your problem to a test case and create a pull request.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Devin Michael

unread,
Mar 10, 2015, 4:18:10 PM3/10/15
to nhu...@googlegroups.com
Sorry for being a newbie.  But what's a pull request?  I pull down a copy of the code put my test case in that code and push it back up?

Devin Michael

unread,
Mar 10, 2015, 7:25:08 PM3/10/15
to nhu...@googlegroups.com
I think I might have solved the issue.  On line 275 of the AuditEventListener class there's the following line of code:

var rd = VerCfg.EntCfg[entityName].GetRelationDescription(referencingPropertyName);

Basically, my object model looked something like this:

ClassA (base class)
ClassB (superclass)

When the GetRelationDescription was being called for entityName = ClassB it was returning NULL for a child bag collection that existed in ClassA.  So ClassB implemented the bag collection but not directly.  Instead the bag collection was actually a property of it's subclass ClassA.  So I simply added a few lines of code below that line that will check the classes the entity inherits from if rd is initially NULL.

I've attached a screenshot highlighting the coding change I made.

I'm not sure if this is a true fix that doesn't break anything but it seems to have resolved the issue I was having with no ill effects so far.


On Monday, March 9, 2015 at 12:26:09 PM UTC-4, Devin Michael wrote:
Changes.png

Ricardo Peres

unread,
Mar 11, 2015, 1:32:16 AM3/11/15
to nhu...@googlegroups.com
You will only know if you integrate your fix with the Envers code and all the unit tests (plus any additional ones that you find necessary) pass.

RP

Roger

unread,
Mar 11, 2015, 5:34:25 AM3/11/15
to nhu...@googlegroups.com
Please report your issue here, https://nhibernate.jira.com/projects/NHE, where you include minimal mapping and configuration to reproduce your problem. 

(
If you want to be more ambitious, add a pull request with a failing test.
If you want to be even more ambitious, add a pull request with a test that fails without your fix and also include a fix that makes your new and all other old tests to pass.

If you are uncertain how to contribute with code to an open source project, the how-to-guide for NH Core is quite good - https://github.com/nhibernate/nhibernate-core/blob/master/CONTRIBUTING.md
Some things differ but the principle is the same for Envers.
)

/Roger

Devin Michael

unread,
Mar 11, 2015, 8:56:21 AM3/11/15
to nhu...@googlegroups.com
I'd be more than happy to take the time to do a pull and create a failing test.  It's the least I could do given all your effort in creating this wonderful library of code.  I'll take a look at the how-to-guide you sent.

Thanks,
Devin

Devin Michael

unread,
Mar 11, 2015, 7:50:05 PM3/11/15
to nhu...@googlegroups.com
Hi Roger,
So I think I've made progress.
1. I created a new issue at the URL you gave me (the issue is NHE-139).
2. I setup a bitbucket account and forked the Envers project (wasn't sure what the best practice was for naming the fork so I simply called it NHibernate.Envers NHE-139)
3. I cloned the fork I created to my local machine

I have a couple of quick questions in terms of best practices:
1. You mentioned I should create a test that fails without my fix and include a fix that makes my new and all old tests pass.  Does that mean I should create two branches in my fork.  The first branch I add a failing test and the second branch I have the same test but with the fix included?  Wasn't sure how to create a failing test and a fix in the same fork since adding the fix would cause the test to pass.

2. Within the Visual Studio solution what is the best place to put my unit test?  Should I create a new folder under the NHibernate.Envers.Test project called "NHE-139" and put my unit test and hbm mapping file example in that folder?

3. Finally, when I write my unit test should I be inheriting from the TestBase (full class name = NHibernate.Envers.Tests.TestBase) class?

Thanks so much.


On Wednesday, March 11, 2015 at 5:34:25 AM UTC-4, Roger wrote:

Roger Kratz

unread,
Mar 12, 2015, 9:45:25 AM3/12/15
to nhu...@googlegroups.com

1.

No, you can keep them in the same commit. I just meant that the test should expose the bug – so without your fix it should fail (if your fix wasn’t there).

 

2.

The namespace Tests\Integration is ported Hibernate Envers tests. In Tests\NetSpecific are tests written for NH Envers. Below this folder there is Integration and UnitTests so place it under Integration please.

 

3.

Please do.

 

Regards

Roger

--

Reply all
Reply to author
Forward
0 new messages