0

I have two model classes ServiceProvider.cs & Club.cs one service provider can have many clubs in it. However ServiceProvider.cs has a virtual collection of clubs in it. As shown below

public class ServiceProvider 
{
    public int ServiceProviderId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNo { get; set; }

    public virtual ICollection<Club> Clubs { get; set; }
}
public class Club 
{
    public int ClubId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public virtual ServiceProvider serviceProvider { get; set; }
    public int ServiceProviderId { get; set; }
}

I have implemented a repository pattern to query the MS Sql server, if i return my repository's _serviceProvider GetAll() method it works fine, and i get all the serviceprovider objects in PostMan.

public IEnumerable<ServiceProvider> Get()
    {
        return _serviceProvider.GetAll();
    }

But if i return the GetAllWithClubs() method of repository then i get run time exception here. Problem is: I am unable to get all service providers along with their clubs collection. However, i have already debugged my code before putting question here, I am getting all the service providers along with their clubs collection perfectly fine. I am sure data fetching from database is fine, problem coming in sending response back from Get() Action method.

public IEnumerable<ServiceProvider> Get()
    {
        return _serviceProvider.GetAllWithClubs();
    }

Error i am getting in Postman is enter image description here output in visual studio is enter image description here

I have tried searched solution of .toList() in front of GetAllWithClubs() plus returning ActionResult which came with Asp.net core 2.1 (Not supported in my case 2.0). I believe this is not a duplicate question. Implementation of GetAll() and GetAllWithClubs() methods are below.

public IEnumerable<ServiceProvider> GetAllWithClubs()
    {
        return _context.ServiceProviders
            .Include(c => c.Clubs);
    }

In Repository.cs

public IEnumerable<T> GetAll()
    {
        return _context.Set<T>();
    }
13
  • 1
    What is the exception? Commented Aug 21, 2018 at 8:59
  • @Stephen Muecke i have edited my question, kindly review now. Commented Aug 21, 2018 at 9:02
  • 1
    Your question still not state what the exception is Commented Aug 21, 2018 at 9:03
  • @StephenMuecke updated question with error Commented Aug 21, 2018 at 9:08
  • @ShahjahanKK "GetAllWithClubs() method of repository then i get run time exception here." which error return in you question you provide error of postman but what exactly error throw from your repository please mention that. Commented Aug 21, 2018 at 9:23

1 Answer 1

0

Problem in above code: is Json .net self referencing loop detection and the solution is that, we have to ignore loop references and not to serialize them For doing this in Asp.net MVC 2.0 we have to put this code in Startup.cs.

services.AddMvc().AddJsonOptions(
                options => options.SerializerSettings.ReferenceLoopHandling =
                Newtonsoft.Json.ReferenceLoopHandling.Ignore);

Rebuild, problem solved.

Sign up to request clarification or add additional context in comments.

3 Comments

A better option would be to create a viewmodel and populate it with the data you want and do not expose your domain model to the outside world.
in that case do we still be needing to ignore loop reference?
It depends on the shape of the data, but you could flatten the data or make the relationship one way which would remove the circular reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.