InvalidCastException when cascading saves to transient dictionary values
Description
When using a <map> collection with cascade="all-delete-orphan" where the index is a simple type and the value is an entity referenced with a one-to-many a InvalidCastException occurs when saving the owning entity.
e.g: var property = session.Get<Property>(propertyId); var item = new Item(); item.Properties.Add(property.Id, new PropertyValue { Value = "Value 1" }); session.Save(item); session.Flush();
The cause seems to be in the IndexOf method in the MapType.cs file where the collection is cast to an IEnumerable - where an IDictionaryEnumerator is expected the default enumerator for a generic dictionary is returned instead.Changing this to do IDictionary iter = (IDictionary)collection; iter.GetEnumerator() correctly returns the IDictionaryEnumerator.
I've tested this with projects targetin .NET 3.5 and .NET 4.0.
Environment
None
Attachments
1
Activity
Show:
Alex Zaytsev
August 2, 2012 at 8:54 AM
Closed after NH 3.3.1.GA release
Alex Zaytsev
May 18, 2012 at 8:31 PM
Commited into 3.3.x branch 81e480992299c153745b5b95f6c25ed2c33d6ed4
Alex Zaytsev
May 9, 2012 at 3:27 PM
There were some problems with attached test case:
incorrect mapping (should use "name" attribute instead of "column" for <id/> element)
When using a <map> collection with cascade="all-delete-orphan" where the index is a simple type and the value is an entity referenced with a one-to-many a InvalidCastException occurs when saving the owning entity.
e.g:
var property = session.Get<Property>(propertyId);
var item = new Item();
item.Properties.Add(property.Id, new PropertyValue { Value = "Value 1" });
session.Save(item);
session.Flush();
The cause seems to be in the IndexOf method in the MapType.cs file where the collection is cast to an IEnumerable - where an IDictionaryEnumerator is expected the default enumerator for a generic dictionary is returned instead.Changing this to do IDictionary iter = (IDictionary)collection; iter.GetEnumerator() correctly returns the IDictionaryEnumerator.
I've tested this with projects targetin .NET 3.5 and .NET 4.0.