Returning children with the first Entity Framework code error: Unable to evaluate expression. The operation is not supported. Unknown error: 0x80070057

I keep getting the following error:

Unable to evaluate expression. The operation is not supported. unknown error: 0x80070057

when trying to return the 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 for testing and in order to show 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; } } 

Children's 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; } } 

return _context.Person works and returns a list of people with zero jobs

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

This, I know, is simple material and only two very simple tables, but I don’t see where the problem is, because I created this senario model countless times without problems. I am thinking about rebuilding the project and EF dependencies, but would prefer to understand this problem and fix it if possible.

+5
source share
1 answer

After some significant hair has pulled out the following problems, follow these steps:

Repository method for _context:

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

The calling code just needs the ToList () method:

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

Now the people variable contains a list of user objects, each of which contains a list of job objects. Pheeew !!

+1
source

Source: https://habr.com/ru/post/1241124/


All Articles