Binary Lion Studios

I code for fun and for food.

NHibernate mapping attributes

1
2
3
4
5
6
System.NullReferenceException: Object reference not set to an instance of an object
  at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindClass (System.Xml.XmlNode node, IDecoratable classMapping, NHibernate.Mapping.PersistentClass model, IDictionary`2 inheritedMetas) [0x00000] in :0 
  at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind (System.Xml.XmlNode node, NHibernate.Cfg.MappingSchema.HbmClass classSchema, IDictionary`2 inheritedMetas) [0x00000] in :0 
  at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses (System.Xml.XmlNode parentNode, IDictionary`2 inheritedMetas) [0x00000] in :0 
  at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind (System.Xml.XmlNode node) [0x00000] in :0 
  at NHibernate.Cfg.Configuration.AddValidatedDocument (NHibernate.Cfg.NamedXmlDocument doc) [0x00000] in :0

I got this stacktrace while trying to get a simple app using NHibernate up and running. I eventually found the solution in this forum post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// [Class(Table="Task")] - This was the problem.
[Class(Name="Project.Models.Task, Project", Table="Task")]
public class Task
{  
    [Id(Name="Id")]
    [Generator(1, Class="native")]
    public virtual string Id { get; set; }
    [Property]
    public virtual string Description { get; set; }
    [Property]
    public virtual bool Complete{ get; set; }
    [Property]
    public virtual DateTime CreatedAt { get; set; }
    [Property]
    public virtual DateTime UpdatedAt { get; set; }
}

I needed to add the FQDN of the class and the assembly name. In the example above, replace Project with your assembly name.