0

I am trying to display a JSON using a JsonResult method in MVC, I am using Entity Framework but my issue is that the PostMan is displaying a server error:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

I am using a query which involves 3 different tables, however one of those tables might retrieve more that 3 different rows.

This is my code:

[HttpGet]
    [AllowAnonymous]
    public JsonResult RetreiveResume(int User)
    {
        using (var context = new DexusEntities())
        {
            var collection = (from p in context.CND_PersonalData join
                              pd in context.CND_ProfessionalData on p.UserId equals pd.UserId join
                              ex in context.CND_ExperiencesData on p.UserId equals ex.UserId select p).ToList();
            return Json(collection, JsonRequestBehavior.AllowGet);
        }
    }

What's wrong with my code?

Thanks in advance.

2 Answers2

0

Move return line after using. You are disposing context just after trying to return results. You can check this link for more information: What happens if i return before the end of using statement? Will the dispose be called?

[HttpGet]
[AllowAnonymous]
public JsonResult RetreiveResume(int User)
{
    var collection = new CND_PersonalData();

    using (var context = new DexusEntities())
    {
        context.Configuration.LazyLoadingEnabled = false; 
        collection = (from p in context.CND_PersonalData join
                          pd in context.CND_ProfessionalData on p.UserId equals pd.UserId join
                          ex in context.CND_ExperiencesData on p.UserId equals ex.UserId select p).ToList();
    }

    return Json(collection, JsonRequestBehavior.AllowGet);
}
Kadir
  • 3,094
  • 4
  • 37
  • 57
  • It says: Cannot convert type System.Collections.Generic.Lis to MySolution.CND_PersonalData –  Mar 27 '19 at 20:14
  • I changed to List collection = new List(); but it's not working. –  Mar 27 '19 at 20:22
  • You still have same error or different one? And your postman http method is get right? – Kadir Mar 27 '19 at 20:50
  • Can you also try to disable lazy loading? context.Configuration.LazyLoadingEnabled = false; – Kadir Mar 28 '19 at 08:15
  • Kadir, your last comment worked perfectly!! context.Configuration.LazyLoadingEnabled = false; solved the problem. I had to do a couple of modifications in the code as well. –  Mar 28 '19 at 18:31
  • Could you please modify your answer to select it as functional? Just copy and past my code and add the context.Configuration.LazyLoadingEnabled = false; before the return. –  Mar 28 '19 at 21:50
-1

could you please try to put the return after the brackets like the following :

using (var context = new DexusEntities())
        {
            var collection = (from p in context.CND_PersonalData join
                              pd in context.CND_ProfessionalData on p.UserId equals pd.UserId join
                              ex in context.CND_ExperiencesData on p.UserId equals ex.UserId select p).ToList();
            return Json(collection, JsonRequestBehavior.AllowGet);
        }