Foreign key reference object is returning null

asp.net-core aspnetboilerplate c# entity-framework-core navigation-properties

Question

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?

1
1
6/7/2018 10:56:28 PM

Accepted Answer

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);
}
5
9/24/2017 3:04:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow