我在具有以下属性定义的实体中引用 AbpUser
表:
[ForeignKey("LocationManagerId")]
public virtual User LocationManager { get; set; }
public virtual long LocationManagerId { get; protected set; }
我的DTO如下:
[AutoMapFrom(typeof(Location))]
public class LocationDto : FullAuditedEntityDto<Guid>
{
// <snip>
public virtual User LocationManager { get; set; }
}
使用 AsyncCrudAppService
并从 MVC Controller 调用以下内容:
var locations = (await _locationAppService.GetAll(new PagedAndSortedResultRequestDto())).Items;
locations.LocationManager
正在返回 null
。我已确认实体和 DTO 中的所有内容都设置为 virtual
。我不知所措。任何人都有任何见解?
最佳答案
如果您使用的是 EntityFrameworkCore,则必须使用预先加载。
在 LocationAppService
中覆盖此方法:
protected override IQueryable<Location> CreateFilteredQuery(LocationGetAllInput input)
{
return Repository.GetAllIncluding(x => x.LocationManager);
}
关于c# - 外键引用对象返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391048/