c# - 外键引用对象返回 null

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

我在具有以下属性定义的实体中引用 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/

相关文章:

c# - 为 Office 创建快捷 Hook 机制的最佳方法是什么

c# - 获取配置文件的路径时使用 AppDomain 和 ConfigurationManager 有什么区别?

c# - 何时子类化以及何时使用对象模型中的属性

c# - AspNet.Core,IdentityServer 4 : Unauthorized (401) during websocket handshake with SignalR 1. 0 使用 JWT 不记名 token

c# - 命令已在进行中

c# - 奇怪的 SQL 错误 int 参数转换为 nvarchar

asp.net-core - 身份 aspnet 核心微服务

c# - 使用 Kudu 在 Azure 上部署 ASP.NET vNext beta 2

c# - 在 Dialog 类中访问 DbContext 的最佳方法是什么

c# - 使用 EF Core 的 Crud Web API C#