Billy Blaze Billy Blaze - 3 months ago 10
ASP.NET (C#) Question

EF 6 not selecting recently updated values unless I rebuild the project

In Visual Studio 2012, I have Project#1 that creates and update entries in a SQL DB using EF 6.

And another Project#2 that is supposed to only select the values created/updated from Project #1

Both projects have a connection string to the local DB

Both projects reference a Data access layer library that contains the .edmx file and the generated models.



The issue is: When I run both applications on different localhost ports I am able to create an entry and select it from the other project

BUT when I want to update an entry I always see the OLD value. And it is only when I rebuild Project #2 in Visual Studio 2012 that I can see the updated value.

Edit action

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Prefix = "paymentform")] PaymentForm paymentform)
{
if (ModelState.IsValid)
{
Payment payment = db.Payments.Find(paymentform.formId);

payment.paymenturl = paymentform.paymenturl;

db.Entry(payment).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Index");
}

return View(new PaymentConfig());
}


What is happening?

Answer

The solution was to avoid using a global variable that initializes the DB context

Here is what I was I doing before in Project#2

public class PayController : Controller
{
    Project2Entities db = new Project2Entities();

    public ActionResult Index(int? id)
    {
      //select
    }
   }

The solution was to simply use using() with the context inside.

    public ActionResult Index(int? id)
    {
        using (var context = new Project2Entities())
        {
            //select here
        }
    }

I have no idea why declaring the context as a global variable doesn't work properly. If anyone knows please clarify.