简体   繁体   English

EF Core:分离的延迟加载导航实体。 为什么?

[英]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.

相关问题 EF Core:分离的延迟加载导航属性 - EF Core: Detached lazy-loading navigation property 具有逻辑和使用导航属性的 EF Core NotMapped Getter - 分离实体不支持延迟加载 - EF Core NotMapped Getter with logic and using navigation properties - lazy-loading is not supported for detached entities EF 延迟加载:将项目添加到导航属性而不加载它 - EF Lazy-Loading: Add item to navigation property without loading it 如何使用EF Core 2.1.0和代理进行延迟加载 - How to make lazy-loading work with EF Core 2.1.0 and proxies EF Core 延迟加载 - 导航属性失败,并显示“命名空间 'Castle.Proxies' 中不存在类型或命名空间名称 'ProductProxy'” - EF Core Lazy-Loading - Navigation properties fail with “The type or namespace name 'ProductProxy' does not exist in the namespace 'Castle.Proxies'” 通过 WCF 服务异步延迟加载分离的自跟踪实体的导航属性? - Asynchronously Lazy-Loading Navigation Properties of detached Self-Tracking Entities through a WCF service? ASP.NET Core Web API 无法返回使用 EF Core 延迟加载提取的结果 - ASP.NET Core Web API can not return results extracted using EF Core lazy-loading 分离的实体或使用“AsNoTracking”加载的实体不支持延迟加载 - Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking' 使用Entity Framework 4.0的延迟加载更新具有导航属性的分离的实体实例 - Updating a Detached Entity Instance with Navigation Properties using Lazy Loading with Entity Framework 4.0 C# 实体框架延迟加载(如果未分离) - C# Entity Framework lazy loading if not detached
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM