1

I have created MVC project with WebApi. And it is working fine but I am getting problem while fetching navigation property (foreign key data) in a details method of simple controller. I came to know that the problem is due to LazyLoading written in below code, in DBEntities constructor.

public DBEntities():base("name=GJ5ServiceProviderDBEntities")       
{
    this.Configuration.LazyLoadingEnabled = false;            
}

When I remove this.Configuration.LazyLoadingEnabled = false; line from above code then it is working fine for MVC project on that time I get all navigation property (foreign key data) but after that I am getting below error for WebApi:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.

Fetching code

public async Task<ActionResult> Details(int? id)
{
      if (id == null)
      {
          return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }

      TableServiceProvider tableServiceProvider = 
          await db.TableServiceProviders.FindAsync(id);

      if (tableServiceProvider == null)
      {
           return HttpNotFound();
      }
      return View(tableServiceProvider);
 }

so is there any solution to solve above both problems ?

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
  • post your fetching code. – Frebin Francis Feb 25 '15 at 06:46
  • @FrebinFrancis i have added fetching code – Govinda Rajbhar Feb 25 '15 at 06:49
  • just create another model , put all needed properties in that model and set the values from TableServiceProvider to the new model. Remove the actionresult return type and replace that with the new model name. – Frebin Francis Feb 25 '15 at 06:54
  • you can refer this link to know how to use include to avoid lazyloading http://stackoverflow.com/questions/10822656/entity-framework-include-multiple-levels-of-properties http://stackoverflow.com/questions/21607455/entity-framework-6-context-not-retrieving-navigation-properties – Frebin Francis Feb 25 '15 at 06:58

1 Answers1

1

First of all I will like to thanks Frebin Francis to his respose and help.

I could solve above problem just adding a single line in my Code.db.Configuration.LazyLoadingEnabled = true;

This code work for me.

    public async Task<ActionResult> Details(int? id)
    {
        db.Configuration.LazyLoadingEnabled = true;
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        TableServiceProvider tableServiceProvider = await db.TableServiceProviders.FindAsync(id);
        if (tableServiceProvider == null)
        {
            return HttpNotFound();
        }
        return View(tableServiceProvider);
    }
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62