0

I'm trying to map a list model object with a child that has reference to the parent. The Json serialization throws a "Self referencing loop detected" error message. My model classes:

public class Event
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<EventElement> EventElements { get; set; }
    ...
}

public class EventElement
{
    public int Id { get; set; }
    ...
    public int EventId { get; set; }
    public virtual Event Event { get; set; }
}

I had tried some tricks in Automapper configuration. First, throw same error: Mapper.CreateMap() .ForMember(vm => vm.EventElements, opt => opt.MapFrom(src => src.EventElements));

Second, return null for each object in the list: Mapper.CreateMap().MaxDepth(1);

How can I get the Event data with children without circular loop?

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
Daniel
  • 137
  • 4
  • 10
  • have you tried ignoring that property, like [here](http://stackoverflow.com/questions/4987872/ignore-mapping-one-property-with-automapper)? – Jonesopolis Oct 06 '14 at 13:50
  • possible duplicate of [JSON.NET Error Self referencing loop detected for type](http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type) – Ben Robinson Oct 06 '14 at 13:50
  • I want ignore the child Event property, I don't know how. I know remove a source member as EventElements in the Event map. I tried Ben Robinson recommendation, it works on fiddler2 but my SPA breaks. – Daniel Oct 06 '14 at 14:05

1 Answers1

1

You need to disable proxy creation in DbContext as below:

  DbContext.Configuration.ProxyCreationEnabled = false;

And use "Include" lambda expression in your repository

public IQueryable<Customer> GetAllCustomers()
    {
        return DbSet.AsQueryable().Include(s => s.StatusType).Include(s => s.CustomerCategory);
    }
Amin Mohammadi
  • 183
  • 2
  • 17
  • Thanks. I found same solution some time ago but I forgot this post. Disable automatic Proxies creation was the solution to this and others loop references issues when two EF classes have a virtual property to link these clases between them, but it forces to include manually all relations to query. – Daniel Feb 01 '16 at 15:26