Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework - navigation property does not load

I have the following relation

enter image description here

public partial class SharedResource : DomainEntity
{
    public System.Guid Id { get; set; }
    public System.Guid VersionId { get; set; }

    public virtual PackageVersion PackageVersion { get; set; } // tried it noth with and without virtual
}

Now, I load SharedResource using

SharedResource sharedResource = Get(shareKey)

And

sharedResource.PackageVersion == null. 

though VersionId is not null and

context.Configuration.LazyLoadingEnabled = false;

What do I have to do in order to load it

like image 214
Bick Avatar asked Apr 15 '13 14:04

Bick


1 Answers

LazyLoadingEnabled must be true, not false:

context.Configuration.LazyLoadingEnabled = true;

true is the default if you don't set LazyLoadingEnabled at all.

And the PackageVersion property must be virtual to enable lazy loading for this property.

Or you can include the property directly in the query:

SharedResource sharedResource = context.SharedResource
    .Include("PackageVersion")
    .SingleOrDefault(s => s.Id == shareKey);
like image 127
Slauma Avatar answered Oct 30 '22 22:10

Slauma