S
S
Saharman2018-11-04 14:00:16
ASP.NET
Saharman, 2018-11-04 14:00:16

Loop when loading related entity, how to fix?

There are two such classes:

public class A
    {
        [Required]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Description { get; set; }

        public List<B> B { get; set; }

        public List<C> C { get; set; }

        public A()
        {
            B = new List<B>();
            C = new List<C>();
        }
    }

public class B
    {
        [Required]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Description { get; set; }

        public List<D> D { get; set; }

        public List<E> E { get; set; }
        [Required]
        public int AId { get; set; }
        
        public A A { get; set; }

        public B()
        {
            D = new List<D>();
            E = new List<E>();
        }

    }

I am using the following upload:
A a = await _context.A
                    .Include(d => d.B).Include(d => d.C)
                    .FirstAsync(d => d.Id == id);

When returning the received value, I get a loop, i.e. class A loads class B, and class B, in turn, loads A again, and A again B, and so on.
Here is an example result:
{
  "code": 0,
  "error": null,
  "data": {
    "id": 2,
    "name": "abc",
    "description": "The",
    "B": [
      {
        "id": 2,
        "name": "ABC",
        "description": "The",
        "D": null,
        "E": null,
        "AId": 2,
        "A": {
          "id": 2,
          "name": "abc",
          "description": "The",
          "B": [

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2018-11-04
@kttotto

Try using Load instead of Include

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question