eoinDeveloper
eoinDeveloper

Reputation: 103

Entity Framework C# - ObjectStateManager

I am finding very hard to find a solution to this problem. I have created an Edit.cshtml view in my MVC application. Currently in my Controller I have the following code

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Vendor vendor)
    {
        if (ModelState.IsValid)
        {
            db.Vendors.Attach(vendor);
            db.ObjectStateManager.ChangeObjectState(vendor, EntityState.Modified);  
            db.Entry(vendor).CurrentValues.SetValues(vendor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(vendor);
    }

But before i build the "dbo.ObjectStateManager" gives my an Error. As below!

Error 25 'VendorScorecard.Models.VendorScorecardEntities1' does not contain a definition for 'ObjectStateManager' and no extension method 'ObjectStateManager' accepting a first argument of type 'VendorScorecard.Models.VendorScorecardEntities1' could be found (are you missing a using directive or an assembly reference?) C:\solutions\Web\VisualStudio2010\VendorScorecard\VendorScorecard-Good\VendorScorecard\Controllers\VendorController.cs 90 20 VendorScorecard

i have tried thid line of code too! It removes the error but doesnt actually allow intput into my database

//db.Entry(vendor).State = EntityState.Modified;

Upvotes: 0

Views: 3028

Answers (2)

Snehal Thakkar
Snehal Thakkar

Reputation: 93

try this code it will resolve your problem

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

so basically you have to get rid of one line and put this code instead please see below

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Vendor vendor)
    {
        if (ModelState.IsValid)
        {
            db.Vendors.Attach(vendor);
             db.Entry(vendor).State = EntityState.Modified;
            db.Entry(vendor).CurrentValues.SetValues(vendor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(vendor);
    }

Upvotes: 0

Neil Smith
Neil Smith

Reputation: 2565

From: Why does the ObjectStateManager property not exist in my db context?

var manager = ((IObjectContextAdapter)dbContext).ObjectContext.ObjectStateManager;

Upvotes: 2

Related Questions