Fluent Nhibernate Composite Key Not Generating Joi

2019-04-13 08:26发布

问题:

I have a mapping with a composite key as below:

CompositeId()
            .KeyReference(x => x.CreatedBy, "member_key")
            .KeyReference(x => x.Box, "box_key");

This works fine for simple gets and inserts, however it is not generating joins with the tables mentioned in the reference where I try and use them as part of a query.

So this:

return _sessionFactory.GetCurrentSession().QueryOver<BoxMember>()
            .Where(x => x.Box.Id == boxId)
            .Where(x => x.Member.DeletedDate == null)
            .Fetch(x => x.Box).Eager
            .Fetch(x => x.CreatedBy).Eager
            .List();

Generates the following SQL:

    SELECT this_.member_key     as member1_5_0_,
       this_.box_key        as box2_5_0_
FROM   box_member this_
WHERE  this_.box_key = '2750e160-ba72-4a70-b554-9fd600e3cfd0' /* @p0 */
and    m1_.deleted_date is null;

回答1:

I had exactly the same issue. Adding Reference mapping helped, but the issue makes no sense anyway:

Try this:

CompositeId() 
            .KeyReference(x => x.CreatedBy, "member_key") 
            .KeyReference(x => x.Box, "box_key"); 
Reference(x => x.CreatedBy);
Reference(x => x.Box);