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?