IQueryable<> 无法正常工作,除非在 Locals 中调试并单击查询和 ResultsView。什么会导致这个?

IQueryable<> not working unless debug and click query and ResultsView in Locals. What would cause this?

我有一个 ResumeBank 实体,其中包含一个 Resume 实体列表。简历中有一个拥有实体的列表。

在我的 DBContext 中:

public DbSet<ResumeBank> ResumeBanks { get; set; }

public DbSet<Resume> BankResumes { get; set; }

并在 OnModelCreating() 中:

    modelBuilder.Entity<Resume>().OwnsMany(s => s.ResumeCategories, a =>
    {
        a.Property<DateTime>("CreatedDate");
        a.Property<DateTime>("UpdatedDate");
        a.ToTable("ResumeCategories");
    });

现在在我的 ResumeBank 存储库 class 中,我有以下内容:

public async Task<ResumeBank> GetResumeBankAsync(int resumeBankID)
{
    IQueryable<ResumeBank> query = ResumeBankContext.ResumeBanks
                                                .Include(c => c.Resumes)
                                                .Include(d => d.ResumeCategories);

    query = query.Where(c => c.Id == resumeBankID);

    return await query.FirstOrDefaultAsync();
}

奇怪的是,当我 运行 它没有断点时,它会在 FirstOrDefaultAsync() 调用上抛出异常。但是,如果我在它上面放一个断点,当它被击中时,我进入我的 Locals windows 并单击 query.ResultsView 展开它,然后按 Continue 它工作!

为什么?

另一个事实是,如果我在 DBContext 的 OnModelCreating() 中注释掉 OwnsMany(),我就没有这个问题。

我正在使用 EntityFrameworkCare 3

例外情况是:

JobAssist.Services.ResumeBankMgmt.Infrastructure.Repositories.ResumeBankRepository: Information: Getting a ResumeBank for 1
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.3\System.Diagnostics.StackTrace.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.3\System.Reflection.Metadata.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'System.NotSupportedException' in System.Private.CoreLib.dll
Exception thrown: 'System.NotSupportedException' in System.Private.CoreLib.dll
Exception thrown: 'System.NotSupportedException' in System.Private.CoreLib.dll
Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor: Information: Executing ObjectResult, writing value of type 'System.String'.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Information: Executed action JobAssist.Services.ResumeBankMgmt.API.Controllers.ResumeBankController.AddResumeAsync (JobAssist.Services.ResumeBankMgmt.API) in 7012.4078ms
Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executed endpoint 'JobAssist.Services.ResumeBankMgmt.API.Controllers.ResumeBankController.AddResumeAsync (JobAssist.Services.ResumeBankMgmt.API)'
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 7155.7843ms 400 application/json; charset=utf-8

也作为另一块拼图:

如果在 GetResumeBankAsync() 中我有:

    IQueryable<ResumeBank> query = ResumeBankContext.ResumeBanks;
    //.Include(c => c.Resumes)
    //.Include(d => d.ResumeCategories);

    query = query.Where(c => c.Id == resumeBankID);

    return await query.FirstOrDefaultAsync();

有效。

以下是模型:

    public class ResumeBank : Entity, IAggregateRoot
    {
        // JobSeeker ID
        public int JobSeekerID { get; private set; }

        // List of Resumes
        private readonly List<Resume> _resumes;

        public IEnumerable<Resume> Resumes => _resumes.AsReadOnly();

        // List of Resume Bank
        private readonly List<ResumeCategory> _resumeCategories;

        public IEnumerable<ResumeCategory> ResumeCategories => _resumeCategories.AsReadOnly();
       }

    public class Resume : Entity
    {
        public string ResumeName { get; private set; }
        public string FileLocation { get; private set; }

        private readonly List<ResumeCategory> _categories;
        public IEnumerable<ResumeCategory> ResumeCategories => _categories.AsReadOnly();
     }

    public class ResumeCategory : ValueObject
    {
        public string CategoryName { get; private set; }
    }

我认为错误源于您的 Resume 模型。 _categories 不是 ResumeCategories 属性 的支持字段的有效名称。根据 docs,您有两个选择:将支持字段名称更新为有效(例如 _resumeCategories),或者在配置中明确指定字段名称:

modelBuilder.Entity<Resume>()
    .Property(b => b.ResumeCategories)
    .HasField("_categories");