I am using the current Version 4.0.3 to map some MySQL tables. However, I already get problems on creating the mapping using an automapper! Reason: A byte array! As soon as I add an byte array like "byte()" (vb.net syntax) I get an exception:
"interface maps for generic interfaces on arrays cannot be retrived."
It seems that this exception comes more deeply from .NET I think but the thing is: I do not have this problem with Fluent nhibernate mapping! Fluent does map my byte array into a longblob which is what I expected to do. However, NHibernate 4 does not. Unfortunately Fluent is not clever enough to map my Id-Properties when they contain some description (e.g. "EmployeeId"). I would need to rename all my properties to "Id" to avoid mapping manually. So because of this I would like to use NHibernates automapper instead but it seems that it cannot handle any kind of array (I also tried using integer arrays as well, same exception). Does anybody have the same problem?? Here is my code:
Public Cfg As Configuration
Public Mapping As HbmMapping
Public SessionFactory As ISessionFactory
Public Sub New()
CreateConfig()
Map()
SessionFactory = Cfg.BuildSessionFactory()
End Sub
Public Sub CreateConfig()
Cfg = New Configuration()
Cfg.DataBaseIntegration(AddressOf DbIntegration)
End Sub
Private Shared Sub DbIntegration(c As IDbIntegrationConfigurationProperties)
c.ConnectionString = "Server=xxx;Port=3306;Database=xxx;User ID=xxx;Password=xxx;"
c.Driver(Of MySqlDataDriver)()
c.Dialect(Of MySQL55Dialect)()
c.LogSqlInConsole = True
c.LogFormattedSql = True
c.AutoCommentSql = True
End Sub
Public Sub Map()
Dim automapper As New ConventionModelMapper()
Mapping = automapper.CompileMappingFor(Assembly.GetExecutingAssembly().GetExportedTypes().Where(Function(x) x.[Namespace].StartsWith("xxx.Models")))
Cfg.AddMapping(Mapping)
End Sub
All this works fine for all my entities except the entities containing array. Actually the only array I need to use is the byte array in a single entity using byte array. I tried to override the mapping using BeforeMapProperty event, however the exception throws anyway because I think the error occurs before that event already.
Thanks! Best, Chris
This happens when the
ConventionModelMapper
is evaluating thebyte[]
to try to figure out if entity →byte
is a one to many relationship. NHibernate sees a collection and attempts to process it as a one-to-many relationship.Obviously this isn't true, since
byte
is obviously not a custom type. In most cases though, it would make sense for NHibernate to evaluate this relationship to try to figure out what the "many" side is of a possible one-to-many relationship.The way to fix this is to create your own model mapper (either inherit from
ModelMapper
orConventionModleMapper
) and define what an "entity" is in your application. This check will exclude thebyte
type and the error should go away.Luckily, this is pretty simple:
And then use
MyModelMapper
instead.In my (limited) experience with automapping, you'll most likely have to override most of the behavior of the convention model mapper. Fortunately with a little trial and error this isn't too hard.