Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8096551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:29:14+00:00 2026-06-05T21:29:14+00:00

I have a simple asp.net MVC4 / EF 4.1 project created with VS 2011,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:29:16+00:00Added an answer on June 5, 2026 at 9:29 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple ASP.Net MVC View which contains an FCKeditor text box (created
I just upgraded my project from ASP.NET MVC1.0 to ASP.NET MVC4.0 One thing that
I have a pretty simple ASP.NET Web Form that looks a bit like the
I created a simple ASP.NET MVC version 1.0 application. I have a ProductController which
I have an Asp .net page like this simple one http://issamsoft.com/app2/page1.aspx and I want
A simple question. I have an ASP.NET web application which contains several assemblies and
I have a partial view (UserControl) that implements a simple pager in my Asp.Net
I have a relatively simple ASP.net MVC 2 app that is using SubSonic. Everything
I have a simple ASP.NET MVC 3 application. I have the database I designed
I have a simple ASP.NET dropdownlist control in a visual studio project (.NET framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.