Domanda

I have a POCO class:

public class Delegate : ConferenceMember
{
    public int? CommitteeMemberCommitteeEntryId { get; set; }

    [Required]
    public int RegistrationId { get; set; }

    public virtual CommitteeMemberCommitteeEntry CommitteeMemberCommitteeEntry { get; set; }

    public virtual Registration Registration { get; set; }

    private Delegate()
    { }

    public Delegate(int conferenceId) : base(conferenceId)
    {

    }
} 

and I reference it in this method:

private bool isHeadDelegate(ConferenceMember item, ApplicationUser user)
    {
        if (item.MemberType == ConferenceMemberType.Delegate)
        {
            if (((xxx.Models.Delegate)item).Registration.RegistrationType == RegistrationType.DelegationRegistration)
            {
                return ((xxx.Models.Delegate)item).Registration.ApplicationUserId == user.Id;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

and even though the RegistrationId is set to a valid object id, the Registration object is not being loaded.

My understanding is that as I have the virtual keyword, the object will be lazy loaded, so it should be available for me to reference, i.e. I am getting a null object reference on the Registration property.

È stato utile?

Soluzione

I needed to change my Delegate() constructor from public to private.

I based my answer on this answer: https://stackoverflow.com/a/22813915/1758319. The constructor must be on the object that contains the object to be lazy-loaded and must be publicly accessible.

i.e.

public Delegate() { }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top