Jonas Vachal
Jonas Vachal

Reputation: 53

Foreign key reference object is returning null

I am referencing the AbpUser table in an entity with the following property definition:

[ForeignKey("LocationManagerId")]
public virtual User LocationManager { get; set; }
public virtual long LocationManagerId { get; protected set; }

My DTO is as follows:

[AutoMapFrom(typeof(Location))]
public class LocationDto : FullAuditedEntityDto<Guid>
{
    // <snip>
    public virtual User LocationManager { get; set; }
}

Using the AsyncCrudAppService and calling the following from an MVC controller:

var locations = (await _locationAppService.GetAll(new PagedAndSortedResultRequestDto())).Items;

locations.LocationManager is returning null. I've confirmed everything in both the entity and the DTO is set to virtual. I'm at a loss. Anyone have any insight?

Upvotes: 1

Views: 1221

Answers (1)

aaron
aaron

Reputation: 43088

If you are using EntityFrameworkCore, you have to use eager-loading.

Override this method in LocationAppService:

protected override IQueryable<Location> CreateFilteredQuery(LocationGetAllInput input)
{
    return Repository.GetAllIncluding(x => x.LocationManager);
}

Upvotes: 5

Related Questions