Jamie Dixon
Jamie Dixon

Reputation: 54021

Retrieving`HasMany` Entities in a single query - Fluent-NHibernate

I have an Entity which holds a collection of OtherEntity in a many-to-many style relationship. The mapping for this property looks like:

HasManyToMany(x => x.OtherEntity)
                .AsBag()
                .Table("EntityToOtherEntityMapping")
                .ParentKeyColumn("EntityId")
                .ChildKeyColumn("OtherEntityId")
                .Not.LazyLoad()
                .Cascade.None();

I notice that when retrieving a collection of Entity's there's a seperate SQL query for each collection of OtherEntity.

How can I have Fluent-NHibernate execute this retrieval in one query rather than n?

Upvotes: 1

Views: 681

Answers (2)

Jamie Dixon
Jamie Dixon

Reputation: 54021

The answer turned out to be in setting the FetchModel to Eager and selecting a ResultTransformer:

.SetFetchMode("Tags", FetchMode.Eager)
.SetResultTransformer(Transformers.DistinctRootEntity)

Upvotes: 0

Gerard
Gerard

Reputation: 2511

Add fetch join on your property.

.Fetch.Join();

Upvotes: 2

Related Questions