a blog for those who code

Wednesday 29 July 2015

[Solved] Duplicate collection role mapping in NHibernate

In this post we are going to discuss about solving Server Error in '/' Application - Duplicate collection role mapping in NHibernate. This is a very common error in NHibernate which happens because two persistent classes with the same unqualified name is specified in your application. This exception will arise if you are attempting to assign two classes to the same name.



The Stack Trace will read like

[DuplicateMappingException: Duplicate collection role mapping ...]
   NHibernate.Cfg.Mappings.AddCollection(Collection collection) +157
   NHibernate.Cfg.XmlHbmBinding.PropertiesBinder.Bind(IEnumerable`1 properties, Table table, IDictionary`2 inheritedMetas, Action`1 modifier, Action`1 addToModelAction) +486
   NHibernate.Cfg.XmlHbmBinding.PropertiesBinder.Bind(IEnumerable`1 properties, IDictionary`2 inheritedMetas, Action`1 modifier) +101
   NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas) +978
   NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas) +80
   NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema) +112
   NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) +204

The solution of this problem is to remove the duplicate enties in your mapping. This problem arises when you mapped your HBM files twice one in Web.Config and also after your configuration.

<!-- HBM Mapping Files -->
<mapping assembly="" />

You can even setup a AutoImport.Never() when configuring your application.

.Mappings(m => m.SomeMappings.Conventions.Setup(a => a.Add(AutoImport.Never())))

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment