Reputation: 22793
Is is possible to use Entity Framework, web API together?
Application Structure:
In my Web API Application I want to use entities from DAL as a model.
Data return from Web API controllers should be JSON.
When I try it always gets error like this:
{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"$id":"2","Message":"An error has occurred.","ExceptionMessage":"Error while copying content to a stream.","ExceptionType":"System.Net.Http.HttpRequestException","StackTrace":null,"InnerException":{"$id":"3","Message":"An error has occurred.","ExceptionMessage":"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.","ExceptionType":"System.ObjectDisposedException","StackTrace":" at System.Data.Objects.ObjectContext.EnsureConnection()\r\n at System.Data.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption)\r\n at System.Data.Objects.ObjectQuery
1.System.Collections.Generic.IEnumerable.GetEnumerator()\r\n at System.Collections.Generic.List1..ctor(IEnumerable
1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper(Object list)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c_DisplayClassd.b_c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"}}}
Here is my controller (Same code is working when I use Code First)
public class TodoController : ApiController
{
// GET api/Todo
public IEnumerable<Todo> GetTodoItems(string q = null, string sort = null,
bool desc = false, int? limit = null, int offset = 0)
{
using (var db = new AdvanceContext())
{
var list = ((IObjectContextAdapter)db).ObjectContext.CreateObjectSet<Todo>();
IQueryable<Todo> items = string.IsNullOrEmpty(sort)
? list.OrderBy(o => o.Priority)
: list.OrderBy(String.Format("it.{0} {1}", sort, desc ? "DESC" : "ASC"));
if (!string.IsNullOrEmpty(q) && q != "undefined")
items = items.Where(t => t.Description.Contains(q));
if (offset > 0)
items = items.Skip(offset);
if (limit.HasValue)
items = items.Take(limit.Value);
return items;
}
}
}
Upvotes: 3
Views: 1411
Reputation: 3
Add following lines in your context:
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = false;
Example:
public partial class Dbname : DbContext
{
public Dbname()
: base("name=Dbname")
{
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = false;
}
public virtual DbSet<dtproperty> dtproperties { get; set; }
Upvotes: 0