Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning child objects with Entity Framework code first error: Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057

I keep getting the following error

Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057

when trying to return a parent object and its children.

The database builds, seeds and has all the relationships defined correctly as far as I can see. I built a smaller model just to test and for the purpose of showing the problem:

Parent object:

public class Person
{
    [Key]
    [Column(Order = 1)]
    public int Id { get; set; }      

    [StringLength(100)]    
    public string Name { get; set; }

    public DateTime DateModified { get; set; }
    public DateTime DateCreated { get; set; }

    public virtual ICollection<Job> Jobs { get; set; }
}

Child object:

public class Job
{
    [Key]
    [Column(Order = 1)]
    public int Id { get; set; }

    [StringLength(100)]
    public string Name { get; set; }    

    public int PersonId { get; set; }

    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }
}

returning _context.Person works and returns the list of person with null jobs

Returning _context.Person.Include(o => o.Jobs) throws the above error.

This I know is simple stuff and only two very simple tables but I cannot see where the problem lies as I have created this model senario countless times without a problem. I am thinking about rebuilding the project and EF dependencies but would prefer to understand this issue and fix it if possible.

like image 274
Nick Parker Avatar asked Jan 21 '16 11:01

Nick Parker


1 Answers

After some considerable hair pulling the following overcame the issue:

The repository method for _context:

  public IQueryable<Person> GetPeople()
    {
        return _context.Person.Include(s => s.Jobs);
    }

The calling code just required a ToList() method:

var people = _repository.GetPeople().ToList();

The people variable now contains a list of person objects each with a list of job objects. Pheeew!!

like image 51
Nick Parker Avatar answered Nov 05 '22 07:11

Nick Parker