0

Here are my classes and mapping overrides, when I call the IsActive method on Member, for some reason nhibernate fetches the MemberExtraFields from the database. This is causing n+1 issues when I call a method on a list.

Is there anything that I could to fix this, I have all laziness (NoProxy, Proxy, False)

public class Member
{
    public virtual Guid Id { get; set; }

    public virtual MemberExtraFields MemberExtraFields { get; set; }


    public virtual bool Enabled { get; set; }

    public virtual bool IsActive()
    {
        return Enabled;
    }

}

public class MemberExtraFields
{
    public virtual Guid Id { get; set; }

    public virtual bool ExcludeFromCompetitions { get; set; }

    public virtual Member Member { get; protected set; }
}

public class MemberMap : IAutoMappingOverride<Member>
{
    public void Override(AutoMapping<Member> mapping)
    {
        mapping.Id(x => x.Id).GeneratedBy.GuidComb();
        mapping.HasOne(x => x.MemberExtraFields)
                        .Cascade.All().LazyLoad(Laziness.NoProxy);
    }
}

public class MemberExtraFieldsMap : IAutoMappingOverride<MemberExtraFields>
{
    public void Override(AutoMapping<MemberExtraFields> mapping)
    {
        mapping.Id(x => x.Id).GeneratedBy.Foreign("Member");
        mapping.HasOne(x => x.Member).Constrained().ForeignKey();
    }
}

1 Answers1

1

Use References instead of HasOne. HasOne is tight coupling.

Note that your IsActive() method returns Enabled property which is associated to your database field and needs to be loaded from there. When NHibernate executes the query to fill that property, it also executes another query to fill MemberExtraFields MemberExtraFields { get; set; } property.

Refer accepted answer here.

Community
  • 1
  • 1
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • Thanks, this actually worked. I had to do tweak the accepted answer to get it working. I had to change to reference on both ends because on database side MemberExtraFields table primary key and foreign key are the same, so when I tried to add a member, nhibernate was trying to add the memberextrafield first before adding member and this was causing "The INSERT statement conflicted with the FOREIGN KEY constraint" issue. To overcome this I had update the mappings to not save MemberExtraFields part of member. – Praga Siva Nov 11 '16 at 03:14