I'm creating another application which shares common entities so I moved the entities and mappings across to this new Common Project. I've changed the namespaces, added the project dependencies and added reference to this new common project but I'm getting the not mapped error. Is there steps I'm missing like adding a reference to it in the startup file or something?
ModelMapper
private ISessionFactory ConfigureNHibernate() {
var cfg = new Configuration();
cfg.DataBaseIntegration(db => {
db.ConnectionString = Configuration.GetConnectionString("MyConnection");
db.Dialect<MsSql2012Dialect>();
db.BatchSize = 500;
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
cfg.AddAssembly("MyProject.Common");
return cfg.BuildSessionFactory();
}
So it looks like the problem is here.
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
cfg.AddAssembly("MyProject.Common");
Assembly.GetExecutingAssembly() isn't going to be your new mapping project assembly.
I always create my domain model in a separate project and pass it in to the nhibernate initializer.
So I've got property on my NhibernateInitializer that takes the mapping assembly
private Assembly MappingAssembly
{
get { return _mappingAssembly ?? (_mappingAssembly = Assembly.Load(_mappingAssemblyName)); }
}
That loads my mapping assembly.
Then when it time to configure them in my mapper the code is
_mapper.AddMappings(MappingAssembly.GetExportedTypes());