views:

850

answers:

2

Is this possible...

MessageMap()
{
    Id(x => x.Id);    
    HasMany(x => x.Folders)
                    .Component(c =>
                               {
                                   c.Map(x => x.Name);
                                   c.References(x => x.User);
                               })
                    .Cascade.AllDeleteOrphan()
                    .Access.AsCamelCaseField(Prefix.Underscore);
}

public class Message
{
    public virtual int Id {get; protected set;}
    public virtual ICollection<Folder> Folders{get; protected set;}
}

public class Folder
{
    public virtual string Name{get;set;}
    public virtual User User{get;set;}
}

With the above mapping I get the following exception...

System.IndexOutOfRangeException: Index was outside the bounds of the array.
[IndexOutOfRangeException: Index was outside the bounds of the array.]
   NHibernate.Mapping.Column.set_Name(String value) +80
   NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindColumns(XmlNode node, SimpleValue model, Boolean isNullable, Boolean autoColumn, String propertyPath) +626
   NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindManyToOne(XmlNode node, ManyToOne model, String defaultColumnName, Boolean isNullable) +33
   NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindComponent(XmlNode node, Component model, Type reflectedClass, String className, String path, Boolean isNullable) +987
   NHibernate.Cfg.XmlHbmBinding.CollectionBinder.BindCollectionSecondPass(XmlNode node, Collection model, IDictionary`2 persistentClasses) +1659
   NHibernate.Cfg.XmlHbmBinding.<>c__DisplayClassd.<AddCollectionSecondPass>b__c(IDictionary`2 persistentClasses) +33
   NHibernate.Cfg.Configuration.SecondPassCompile() +118
   NHibernate.Cfg.Configuration.GenerateDropSchemaScript(Dialect dialect) +44
   NHibernate.Tool.hbm2ddl.SchemaExport..ctor(Configuration cfg, IDictionary`2 connectionProperties) +53
   NHibernate.Tool.hbm2ddl.SchemaExport..ctor(Configuration cfg) +9
A: 

I think your component should be mapped in the mapping for the Folder object. As written, it looks like you are trying to add a component to the Folders collection which doesn't make sense. I'm surprised this compiled.

Jamie Ide
Sorry, I've edited the question to give a bit more information.
Oll
Your edit gives me more confidence in my answer. You can't have a component on a collection, it has to be on the class that is in the collection.
Jamie Ide
See: http://stackoverflow.com/questions/814931/nhibernate-fluentnhibernate-property-bagThe collection of components is exactly the same. I think the issue is with c.References(x => x.User). Removing that line prevents the above exception but obviously causes problems later on.Thanks.
Oll
A: 

The problem is with c.Map(x => x.Name) and c.References(x => x.User). The lambdas should use c as that is the component class.

Id(x => x.Id);    
HasMany(x => x.Folders)
                .Component(c =>
                           {
                               c.Map(c => c.Name);
                               c.References(c => c.User);
                           })
                .Cascade.AllDeleteOrphan()
                .Access.AsCamelCaseField(Prefix.Underscore);
Lachlan Roche