网页API不是从EF 4.1的DbContext返回新添加的记录 [英] Web api not returning newly added records from EF 4.1 DbContext

查看:225
本文介绍了网页API不是从EF 4.1的DbContext返回新添加的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2011 VS创建了一个简单的asp.net MVC4 / EF 4.1项目,一层为我的域模型,一个是我的数据库包含的DbContext。我有一个基本的领域类称为批处理和使用指标与标准的CRUD功能的BatchController /创建/编辑操作。我想补充两个默认的记录与被覆盖的种子的方法。这一切工作正常,我可以使用开箱MVC模板的添加/编辑/删除记录:

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);
    }
}

我添加了一个新的MVC4的Web API项目与暴露域对象,这样数据可以通过JSON检索的意图的解决方案。它使用一个API控制器,​​我已经叫BatchesController,我增加了一个参考我的域名和数据库层。我有两个Get()方法,一个返回所有批次和一个返回给一个id一个批次。我使用的是IIS防爆preSS主办的主要MVC应用程序和Web API。要检索所有批次我在浏览器中运行以下命令:

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

下面是我的Web API控制器:

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;
    }
}

我的问题是,当我添加一个新的记录,并尝试通过浏览器来获取它,只有现有记录交锋与返回种子的方法 - 我不能退换任何新添加的记录。该的DbContext似乎缓存初始记录,并不会到数据库来获取最新的...我怎么返回新添加的记录?

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?

推荐答案

只是为了清除出明显的,你一定会重新连接为网络API项目指向同一个数据库,对不对?因为默认情况下的Web API会重视自己的SQL精简DB。这意味着你可以有效地使用2个不同的数据库

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

这篇关于网页API不是从EF 4.1的DbContext返回新添加的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆