views:

1494

answers:

2

Hi all

I am getting the following runtime error in my ASP.Net MVC application:

NHibernate.MappingException: No persister for: MyProject.Model.MyDomainObject

I am referencing the burrow and fluent binaries in my application and am reconfiguring burrow in Global.asax on Application_Start as follows:

var bf = new BurrowFramework();
IFrameworkEnvironment fe = bf.BurrowEnvironment;
Configuration cfg = fe.GetNHConfig("PersistenceUnit1");
cfg.AddMappingsFromAssembly(Assembly.LoadFrom(Server.MapPath("~/bin/MyProject.Data.dll")));
fe.RebuildSessionFactories();

I cannot for the life of me figure this out. If I setup a fluent NHibernate AutoPersistenceModel with my domain objects then everything works fine, it just doesn't work for manually configured fluent maps.

The single map I have is as follows:

public class MyDomainObjectMap : ClassMap<MyDomainObject>
{
 public MyDomainObjectMap()
 {
  WithTable("my_domain_object");
  Id(x => x.Id);
  Map(x => x.Name);
  Map(x => x.Description);
 }
}

Any help would be much appreciated, please let me know if you need more detail.

Thanks

A: 

Ok I have got it to work by doing the following:

var bf = new BurrowFramework();
IFrameworkEnvironment fe = bf.BurrowEnvironment;
Assembly assembly = Assembly.LoadFrom(Server.MapPath("~/bin/MyProject.Data.dll"));
Configuration cfg = fe.GetNHConfig("PersistenceUnit1");

Fluently.Configure(cfg)
    .Mappings(m => m.FluentMappings.AddFromAssembly(assembly))
    .BuildConfiguration();

fe.RebuildSessionFactories();

Does anyone know why my previous approach didn't work?

Thanks

In your original post you just called cfg.AddMappingsFromAssembly rather than using the Fluently.Configure process in your last.
jasonlaflair
Hi Jason, AddMappingsFromAssembly is a Fluent extension method that (I assumed) pulls in fluent maps.
+3  A: 

To answer why your initial approach failed, cfg.AddMappingsFromAssembly() scans the target assembly for preconfigured, embedded XML mapping files built into the assembly. Since you are generating the mappings 'Fluently' at runtime, these XML files do not exist.

The following, on the other hand, reflects over the assembly to find for your defined 'FluentMappings' (i.e. those deriving from ClassMap<> ), generates the mapping dynamically, and injects it into the configuration. The mappings do not exist until you call into FluentMappings.AddFromAssembly()

Fluently.Configure(cfg)
    .Mappings(m => m.FluentMappings.AddFromAssembly(assembly))
Kevin Pullin