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.
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With