简体   繁体   中英

EF Core: detached lazy-loading navigation entity. Why?

I have the following model:

public partial class Device
{
    public int Id { get; set; }
    public virtual Tablet Tablet { get; set; }

where Tablet is the following:

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:

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:

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'. Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking()'.'. This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

why it's detached?

Is the error occurring on the var devices = new PagedList(...) or later, such as when the controller method completes? 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.

Normally PagedList would utilize a ToPagedList(page, pageSize) method when working with IQueryable which would trigger a one-off load of a Page. If written as a class initialized with an IQueryable , a data retrieval query could be non-fillable if queried after the DbContext is disposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM