Web api does not return newly added records from EF 4.1 DbContext

I have a simple asp.net MVC4 / EF 4.1 project created using VS 2011, with a layer for my domain model and one for my database that contains DbContext. I have one base class for the Batch and BatchController domains with standard CRUD functionality using Index / Create / Edit operations. I add two default entries with an overridden Seed method. It all works great. I can add / edit / delete entries using the MVC template from the box:

public class BatchController : Controller { private readonly MyContext _context = new MyContext(); public ActionResult Index() { return View(_context.Batches.ToList()); } [HttpPost] public ActionResult Create(Batch batch) { if (ModelState.IsValid) { this._context.Batches.Add(batch); this._context.SaveChanges(); return RedirectToAction("Index"); } return View(batch); } } 

I added a new MVC4 Web api project to a solution with the intention of exposing a domain object so that data can be obtained via json. This uses an api controller, which I called BatchesController, and added a link to my domain and database levels. I have two Get () methods, one of which returns all parties and one to return one party with identifier. I use IIS Express to host the main MVC application and web api. To get all the batches, I run them in a browser:

 http://localhost:46395/api/batches 

Here is my web api controller:

 public class BatchesController : ApiController { private readonly MyContext _context; public BatchesController() { _context = new MyContext(); } // GET /api/batches public IEnumerable<Batch> Get() { var batches = _context.Batches.ToList(); if (batches == null) throw new HttpResponseException(HttpStatusCode.NotFound); return batches; } // GET /api/batches/5 public Batch Get(int id) { var batch = _context.Batches.Find(id); if (batch == null) throw new HttpResponseException(HttpStatusCode.NotFound); return batch; } } 

My problem is that when I add a new record and try to get it through the browser, only the existing records associated with the Seed method are returned - I can’t get the new added record that needs to be returned. It seems that DbContext caches the initial records and does not go to the database to get the last ... how to return the newly added records?

+1
asp.net-mvc asp.net-web-api entity-framework
source share
2 answers

Just to eliminate the obvious, you definitely went to the web API project to point to the same database, right? Because by default, the Web API will attach its own SQL Compact DB. This means that you can effectively use two separate databases.

0
source share

There is an answer that does not solve my problem: http://www.strathweb.com/2012/03/serializing-entity-framework-objects-to-json-in-asp-net-web-api/

In addition, the same question arises here: http://forums.asp.net/t/1814377.aspx/1?Web+api+not+returning+records+from+EF+4+1+DbContext

and I find this useful: ASP.Net Web API rendering correctly in VS but providing HTTP500

BUT POINT: You cannot send a proxy object to the webapi serializer. Thus, this should be a project for a new dynamic class or a predefined class in which there is no virtual one (or, possibly, IList, ICollection, ...).

 // GET api/ProfileGame public dynamic GetProfileGames() { return db.ProfileGames.Select(pg => new { ... }).AsEnumerable(); } 
0
source share

All Articles