2

I'm trying to map a many-to-many collection with Fluent NHibnernate. My model class has this property:

public virtual IList<Resource> Screenshots
{
    get { return _screenshots; }
    protected set { _screenshots = value; }
}

And my fluent mapping is:

HasManyToMany(x => x.Screenshots)
                .AsList(x => x.WithColumn("Index"))
                .Cascade.AllDeleteOrphan();

When I run my application, I get the following exception message:

The element 'list' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'many-to-many' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'index, list-index' in namespace 'urn:nhibernate-mapping-2.2'.

There should be a way to do this. Does anyone know what I am doing wrong?

3
  • Are you using a link table for your many to many relation?
    – gcores
    Mar 15, 2009 at 0:00
  • I have since discovered that this was a bug in Fluent NHibnernate. It is documented as fixed in the latest build. Apr 24, 2009 at 20:32
  • 1
    You may want to answer this yourself with the link to that bug report and any applicable version numbers.
    – Jon Adams
    Jan 26, 2011 at 2:05

1 Answer 1

3

The current FluentNHibernate syntax for this is as follows:

HasManyToMany(x => x.Screenshots)
    .AsList(i => i.Column("`Index`"));

The index column defaults to Index, but that's a reserved word on SQL Server (and probably other databases too), so you must quote it with back-ticks.

Also, I'd recommend against setting a cascade on this relationship. Consider the following code:

x.Screenshots.Remove(s);
session.SaveOrUpdate(x);

NHibernate will correctly delete rows from the linking table even without a cascade specified. However, if you specify AllDeleteOrphan, then NHibernate will delete the row from the linking table and also delete Resource s. I doubt this is the behavior you want on a many-to-many relationship.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.