Beengie
Beengie

Reputation: 1608

System.Data.Entity returned instead of namespace.Models on JsonResult

Tables with no relationships will return the Json correctly from collection objects referenced as AppName.Models.ModelName.

However, tables with relationships do not return the data correctly from collection objects referenced as System.Data.Entity.DynamicProxies.ModelName_{GUID}.

in controller...

public ActionResult GetTableWtihNoRelationship()
{
  return Json(db.TWNR);
  // returns the json object with no errors.
}

public ActionResult GetTableWithRelationship()
{
  return Json(db.TWR);
  // returns a 505 error
}

The error produced seems to be on the controller side when the objects are going through serialization (when stepping through debugging).

What causes this problem?

Edit...

Thanks Slicksim for providing the answer. For those who are looking to change the tt template so you don't need to update it each time the Model1.Context.tt is autogenerated, check this SO answer...

Upvotes: 0

Views: 35

Answers (1)

Slicksim
Slicksim

Reputation: 7172

Have you tried to turn off the proxy creation when you create the EF Context?

public YourContext(string connectionString) : base(connectionString)
{
    this.Configuration.ProxyCreationEnabled = false;
}

Upvotes: 1

Related Questions