2
votes

I have the following mapping for a Relation in Castle AR

[BelongsTo("EVENT_ID", Lazy = FetchWhen.OnInvoke)]
        public EventType PayEvent
        {
            get
            {
                return m_PayEvent;
            }
            set
            {
                m_PayEvent = value;
            }
        }

But the Relation is fetched even if the property is not invoked.Is there anything missing here? I am using SessionScope as well.

3
I have tried this one but it does not work for me. - Amitabh

3 Answers

5
votes

It works for me. Make sure you have the entity marked as lazy and the properties and methods are all virtual.

0
votes

in addition lazy-loading for BelongsTo relations does not work if you set NotFoundBehaviour to Ignore

Sample:

[BelongsTo("EVENT_ID", Lazy=FetchWhen.OnInvoke, NotFoundBehaviour:=NotFoundBehaviour.Ignore)] 
-3
votes

You cannot enable lazy loading with belogs to relationship.
See here.
You can implement it yourself.
Store the ID in your model and then:

    public ServicePlan PreviousServicePlan
    {
        get
        {
           if (previousServicePlan == null)
                previousServicePlan = ActiveRecordMediator<ServicePlan>
                    .FindByPrimaryKey(PreviousServicePlanId, false);

           return previousServicePlan;
        }

        private set 
        {
            previousServicePlan = value;
        }
    }