Adam Albrecht
Adam Albrecht

Reputation: 6870

Where clause in Fluent NHibernate Many-to-Many

I am trying to setup a many-to-many mapping in Fluent Nhibernate that has a where clause attached to the child table.

This is basically how it should work:

HasManyToMany(p => p.Images)
  .Table("ProductImages")
  .ParentKeyColumn("ProductID")
  .ChildKeyColumn("ImageID")
  .Where("ImageTypeID = 2");

The ImageTypeID column is in the Images table, but NHibernate is assuming it is part of the ProductImages table. Any idea how I can specify this?

Thanks!

Upvotes: 4

Views: 2194

Answers (2)

autonomatt
autonomatt

Reputation: 4433

You can. Use .ChildWhere in your Fluent NHibernate mapping:

HasManyToMany(p => p.Images)
  .Table("ProductImages")
  .ParentKeyColumn("ProductID")
  .ChildKeyColumn("ImageID")
  .ChildWhere("ImageTypeID = 2");

Upvotes: 10

Jamie Ide
Jamie Ide

Reputation: 49251

You can't as far as I know. I'm not sure that Where is valid on a many-to-many association.

I would handle this by creating an extension method on IEnumerable<Image> to allow easy filtering on image type. Then you could call Images.Landscape(), e.g., on any Images collection.

Upvotes: 0

Related Questions