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:
Here is my web api controller:
public class BatchesController : ApiController { private readonly MyContext _context; public BatchesController() { _context = new MyContext(); }
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?
asp.net-mvc asp.net-web-api entity-framework
Ciaran bruen
source share