Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

how to fix JSON Self Referencing Loop Exceptions

$
0
0

My Project in asp.net core 3.0 .Net core framework 

I have the following  model.

When I run the query in postman the following error is coming 'JSON Self Referencing Loop Exceptions'  when I try to include the  Icollection list Gigs . Please help

public class Event
    {
        public int EventId { get; set; }
        public string EventName { get; set; }
        public DateTime EventDate { get; set; }

        [ForeignKey("VenueId")]
        public int VenueId { get; set; }
        public Venue Venue { get; set; }
        public  ICollection<Gig> Gigs { get; set; }
    }

public class Comedian
    {
        public int ComedianId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string ContactPhone { get; set; }
    }

 public class Gig
    {
        public int GigId { get; set; }
        public string GigHeadline { get; set; }
        public int GigLengthInMinutes { get; set; }

        [ForeignKey("EventId")]
        public int EventId { get; set; }
        public Event Event { get; set; }

        [ForeignKey("ComedianId")]
        public int ComedianId { get; set; }
        public Comedian Comedian { get; set; }
    }

public class Venue
    {
        public int VenueId { get; set; }
        public string VenueName { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public int Seating { get; set; }
        public bool ServesAlcohol { get; set; }
    }

I have the following record

Event
EventId	EventName		EventDate			VenueId
1	Funny Comedy Night	2019-05-19 00:00:00.0000000	1
4	Funny Comedy Night2	2019-05-19 00:00:00.0000000	1


Gig
GigId	GigHeadline		GigLengthInMinutes	EventId		ComedianId
1	Pavols Funny Show	60			1		1
2	Lifetime Of Fun	45	1			2


Venue
VenueId	VenueName	Street			City		State	ZipCode	Seating	 ServesAlcohol
1	Mohegan Sun	123 Main Street		Wilkes Barre	PA	18702	125	   1


Comedian
ComedianId	FirstName	LastName	ContactPhone
1		Pavol		Almasi		111-222-3333
2		Robin		Williams	444-555-6666

My code is given below

[HttpGet("{eventId:int}")]
        public async Task<ActionResult<EventDto>> GetEvent(int eventId, bool includeGigs = true)
        {
            try
            {
                var result = await _eventRepository.GetEvent(eventId, includeGigs);

                if (result == null) return NotFound();

                var mappedEntity = _mapper.Map<EventDto>(result);
                return Ok(mappedEntity);
            }
            catch (Exception)
            {
                return this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure");
            }
        }


Repo Sql  
public async Task<Event> GetEvent(int eventId, bool includeGigs = false) { _logger.LogInformation($"Getting event for event id {eventId}"); IQueryable<Event> query = _db.Events .Include(v => v.Venue); if (includeGigs) { query = query.Include(g => g.Gigs) .ThenInclude(c => c.Comedian); } query = query.Where(e => e.EventId == eventId); return await query.FirstOrDefaultAsync(); }
in Startup.cs
 public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<IEventRepository, EventRepository>();

          
            services.AddControllers();

        }




Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>