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

382 Views Asked by At

I have a entity of ResumeBank that contains a list of Resume entities. The Resume has a list of owned entities.

In my DBContext:

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

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

and in the OnModelCreating():

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

Now in my Respository class for ResumeBank I have the following:

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();
}

The wierd things is when I run it without a breakpoint it throws an exception on the FirstOrDefaultAsync() call. However if I put a breakpoint on it and when it is hit I go into my Locals windows and click on query.ResultsView to expand it and then press Continue it works!!

Why?

Also another fact is, if I comment out the OwnsMany() in the OnModelCreating() in DBContext, I dont have this issue.

I am using EntityFrameworkCare 3

enter image description here

The Exception is:

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\3.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\3.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

Also as another piece of the puzzle:

If in the GetResumeBankAsync() i have:

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

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

    return await query.FirstOrDefaultAsync();

it works.

Here are the models:

    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; }
    }
1

There are 1 best solutions below

1
On BEST ANSWER

I believe the error stems from your Resume model. _categories is not a valid name for a backing field of the ResumeCategories property. According to the docs, you have two options: Update the backing field name to be valid (e.g. _resumeCategories), or explicitly specify the field name in the configuration:

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