流畅的nhibernate映射组件与引用

时间:2009-05-29 10:45:39

标签: nhibernate fluent-nhibernate

这可能......

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;}
}

通过上面的映射,我得到以下异常......

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

2 个答案:

答案 0 :(得分:1)

问题在于c.Map(x => x.Name)c.References(x => x.User)。 lambdas应该使用c,因为它是组件类。

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);

答案 1 :(得分:0)

我认为您的组件应该映射到Folder对象的映射中。如上所述,看起来您正在尝试将一个组件添加到Folders集合中,这是没有意义的。我对此编译感到惊讶。