Web api not returning newly added records from EF

2019-08-08 07:04发布

I have a simple asp.net MVC4 / EF 4.1 project created with VS 2011, with a layer for my domain model and one for my database that contains the DbContext. I have one basic domain class called Batch and a BatchController with the standard CRUD functionality using Index / Create / Edit actions. I add two default records with the overridden Seed method. All this works fine I can add / edit / delete records using the out of the box MVC template:

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 the solution with the intention of exposing the domain object so the data can be retrieved via json. This uses an api controller that I've called BatchesController, and I added a reference to my domain and database layers. I have two Get() methods, one to return all Batches and one to return a single batch given an id. I'm using IIS Express to host the main MVC app and the Web api. To retrieve all the Batches I run this in a browser:

http://localhost:46395/api/batches

Here's 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 retrieve it via a browser, only the existing records aded with the Seed method are returned - I can't get any newly added record to be returned. The DbContext seems to be caching the initial records and not going to the database to get the latest...how do I return newly added records?

2条回答
Summer. ? 凉城
2楼-- · 2019-08-08 07:35

Just to clear out the obvious, you have surely rewired to Web API project to point to the same database, right? Because by default Web API will attach its own SQL Compact DB. Meaning that you could effectively be using 2 separate databases

查看更多
三岁会撩人
3楼-- · 2019-08-08 07:37

There is an answer, which It doesn't solve my problem: http://www.strathweb.com/2012/03/serializing-entity-framework-objects-to-json-in-asp-net-web-api/

Also, there is a same question at 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 showing correctly in VS but giving HTTP500

BUT THE POINT IS: You can not send the proxy object to webapi serializer. So it should be project to a new dynamic class or a predefined class which there is no virtual (or maybe IList, ICollection,...).

// GET api/ProfileGame
public dynamic GetProfileGames()
{
    return db.ProfileGames.Select(pg => new
    {
        ...
    }).AsEnumerable();
}
查看更多
登录 后发表回答