1

I am trying to get the students from the database and also include the course entities corresponding to them, but it seems that I'm doing something wrong.

this is student class:

public class Student
{
    public int StudentID { get; set; }

    public string Name { get; set; }

    public int CourseID { get; set; }  

    public virtual Course Course { get; set; }
}

this is course class:

public class Course
{
    public int CourseID { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

and I retrive the collection of student entities as shown below:

context.Students.Include(i => i.Course).ToList();

If I remove the include method, then I get data, but course property of student object is null.

P.S I am testing it with Postman and with "Include" I am not able to get anything.

If I comment this

public virtual ICollection<Student> Students { get; set; }

everything works fine.

I put the full code on github:

https://github.com/AlexDev5/Problem

A.M
  • 731
  • 2
  • 7
  • 13

2 Answers2

0

You have to configure the serializer to ignore circular references in your project.

So for this you have to add following line of code inside ConfigureServices method in Startup.cs

Like

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
    .AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
}
er-sho
  • 9,581
  • 2
  • 13
  • 26
  • I think, my situation is a bit different. In older versions, I had not problem too.. – A.M Jul 22 '18 at 13:23
  • @A.M., see the result in image. – er-sho Jul 22 '18 at 13:25
  • Are you testing it in ASP.NET Core WEB API 2 with Entity Framework core? maybe there is difference – A.M Jul 22 '18 at 13:31
  • I have a project with web api not core and its working well – er-sho Jul 22 '18 at 13:33
  • are you using in web api core? – er-sho Jul 22 '18 at 13:37
  • Yes, I am. I debuging my code now and I think that there is a problem with the getter of Students property in course class.. – A.M Jul 22 '18 at 13:40
  • @A.M., please view edited answer may it help you. – er-sho Jul 22 '18 at 14:37
  • I have project where objects have huge amount of properties, so I think this won't be best solution for this situation (in this case more code is needed to be written). I have put the code on github: https://github.com/AlexDev5/Problem – A.M Jul 22 '18 at 16:14
  • Then, Did you got any solution? – er-sho Jul 22 '18 at 16:50
  • Yes, I updated the ConfigureServices method in StartUp.cs file..The problems was that I wasn't handling Json reference looping – A.M Jul 22 '18 at 17:49
  • Ohh well done :) – er-sho Jul 23 '18 at 00:56
  • @A.M. I asked my co-worker about you problem and they gave me a simple solution, I update my answer now, please view updated answer, may it help you. :) – er-sho Jul 23 '18 at 05:37
  • Yes, this is the solution. the question referenced by Ivan Stoev has the same solution. I solved my problem in this way. – A.M Jul 24 '18 at 11:52
  • @A.M, glad to hear you have get rid from your problem :) – er-sho Jul 24 '18 at 12:24
  • Thank you. Do you know WPF? I have one question.... https://stackoverflow.com/questions/51498387/bind-selected-item-to-combobox-with-caliburn-micro – A.M Jul 24 '18 at 14:53
0

Depending on the sample found here https://github.com/aspnet/EntityFramework.Docs/tree/master/samples/core/Querying/Querying

I wrote one solution, but I don't know if it is good approach for this problem:

I created StudentCourse class and defined it as shown below:

public class StudentCourse
{
    public int StudentCourseID { get; set; }

    public int StudentID { get; set; }
    public Student Student{ get; set; }

    public int CourseID { get; set; }
    public Course Course { get; set; }
}

And I modified the Course class as shown below:

public class Course
{
    public int CourseID { get; set; }

    public string Name { get; set; }

    public virtual ICollection<StudentCourse> Students { get; set; }
}
A.M
  • 731
  • 2
  • 7
  • 13
  • But this is totally different (many-to-many) relationship with additional link table. While the original design is one-to-many. `Include` should work correctly in both cases, except if your database schema doesn't match the model. – Ivan Stoev Jul 22 '18 at 15:08
  • @Ivan Stoev, I tested it in debug mode several times and the problem arises when debugger comes to `get` in `public virtual ICollection Students { get; set; }`. So, If I remove this line of code, problem dissapears.. – A.M Jul 22 '18 at 15:18
  • I have put the source code on github... https://github.com/AlexDev5/Problem – A.M Jul 22 '18 at 16:11
  • *"If I remove "Students" virtual property from the Course class, I get what I need."* - then your problem has nothing to do with EF Core, but the Json serialization of objects with circular references. There are plenty of posts here on SO of how to configure Json reference loop handling. For instance, the one that I marked as duplicate. – Ivan Stoev Jul 22 '18 at 16:37
  • thank you, I will read about it... – A.M Jul 22 '18 at 16:42