[英]EF Core: detached lazy-loading navigation entity. Why?
I have the following model:我有以下 model:
public partial class Device
{
public int Id { get; set; }
public virtual Tablet Tablet { get; set; }
where Tablet
is the following:其中Tablet
如下:
public class Tablet
{
public string TabletId { get; set; }
public int DeviceId { get; set; }
public virtual Device Device { get; set; }
private ICollection<TabletTransferRequest> _tabletTransferRequests;
public virtual ICollection<TabletTransferRequest> TabletTransferRequests { get => _tabletTransferRequests ?? (_tabletTransferRequests = new List<TabletTransferRequest>()); protected set => _tabletTransferRequests = value; }
}
and the mapping class:和映射 class:
public class TabletMap : IEntityTypeConfiguration<Tablet>
{
public void Configure(EntityTypeBuilder<Tablet> builder)
{
builder.ToTable(nameof(Tablet));
builder.HasKey(p => p.TabletId);
builder.HasOne(p => p.Device)
.WithOne(o => o.Tablet)
.HasForeignKey<Tablet>(p => p.DeviceId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict)
;
}
}
DTO classes: DTO 类:
public class DeviceDisplayDto
{
public int Id { get; set; }
public TabletPartDto Tablet { get; set; }
}
public class TabletPartDto
{
public string TabletId { get; set; }
public List<TabletTransferRequestElementDto> TabletTransferRequests { get; set; }
}
public class TabletTransferRequestElementDto : DeviceRequestElementAbstractDto
{
public string TabletId { get; set; }
public int DeviceId { get; set; }
}
when I try to do the following当我尝试执行以下操作时
var query = _context.Devices.Include(d => d.Tablet).ThenInclude(d => d.TabletTransferRequests);
var devices = new PagedList<DeviceDisplayDto>(query.ProjectTo<DeviceDisplayDto>(_mapperConfig), pageIndex, pageSize);
I got the following:我得到以下信息:
Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning: An attempt was made to lazy-load navigation property 'TabletTransferRequests' on detached entity of type 'TabletProxy'.为警告“Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning”生成错误:尝试在“TabletProxy”类型的分离实体上延迟加载导航属性“TabletTransferRequests”。 Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking()'.'.分离实体或使用“AsNoTracking()”加载的实体不支持延迟加载。 This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.通过将事件 ID 'CoreEventId.DetachedLazyLoadingWarning' 传递给 'DbContext.OnConfiguring' 或 'AddDbContext' 中的 'ConfigureWarnings' 方法,可以抑制或记录此异常。
why it's detached?为什么它是分离的?
Is the error occurring on the var devices = new PagedList(...)
or later, such as when the controller method completes?错误是否发生在var devices = new PagedList(...)
或更高版本上,例如 controller 方法完成时? I don't recognize that PagedList implementation, but from what you've typed that rendition appears to be initializing with IQueryable<T>
rather than a static collection IList<T>
which may be an issue if the PagedList
ends up attempting to resolve paging from the IQueryable
after an initial load.我不认识 PagedList 实现,但是从您输入的内容来看,该再现似乎是使用IQueryable<T>
而不是 static 集合IList<T>
进行初始化的,如果PagedList
最终尝试解决分页问题,这可能是一个问题在初始加载后从IQueryable
中获取。
Normally PagedList would utilize a ToPagedList(page, pageSize)
method when working with IQueryable
which would trigger a one-off load of a Page.通常 PagedList 在使用IQueryable
时会使用ToPagedList(page, pageSize)
方法,这会触发页面的一次性加载。 If written as a class initialized with an IQueryable
, a data retrieval query could be non-fillable if queried after the DbContext is disposed.如果编写为使用IQueryable
初始化的 class ,则在释放 DbContext 后查询数据检索查询可能是不可填充的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.