0

I have an issue with my method that updates the database with user information. during HttpGet it uses a view model to ensure the integrity of the data, and in HttpPost, the data is passed back and is there. (ran some checks with breakpoints and everything was holding the correct user data that they should have been)

However, when I run my .Save() method, this information is not stored into the DB.

I've checked to see if its pointing to the correct database by changing the data manually, it comes up in a list view just fine. I'M missing something, just can't figure out what ><

Below is the code there is form data in all of the relevant view models, but it just doesn't save!

    [HttpPost]
    public ActionResult HouseCreate(CreateHouseViewModel viewModel)
    {

        if (ModelState.IsValid)
        {          
            var house = new House();
            house.address = viewModel.address;
            house.postCode = viewModel.postCode;
            house.noOfDisputes = viewModel.noOfDisputes;
            _db.Save();

          return RedirectToAction("House");
        }
        return View(viewModel);
    }

house is an object of my house database, modelled correctly with correct Primary keys in place (double checked this in the database viewer.) save calls:

void DataSources.Save()
    {
        SaveChanges();
    }

No errors come up, which makes it even worse.

1
  • 7
    Well, you don't seem to be adding your "house" to your database anywhere. You're just create a new House and calling Save. What is Save() anyways? It's not Entity Framework, as that is SaveChanges(). Commented Jan 29, 2013 at 18:38

4 Answers 4

4
using (var context = new UnicornsContext())
{
    var unicorn = new Unicorn { Name = "Franky", PrincessId = 1};

    context.Unicorns.Add(unicorn); // your missing line

    context.SaveChanges();
}

Source

Sign up to request clarification or add additional context in comments.

Comments

0

Your "_db" object, I assume is entity framework. This will have a collection on it, maybe _db.Houses ? You need to add the object to that.

1 Comment

This points back to my DataSources class which holds IQueryable collections of all the tables i'll be querying, when i specify the Houses database though, I can't seem to call the save method. I'm missing something obvious I can feel it.
0

Since it appears you are using Entity Framework, you'll need to inform Entity Framework that your model has been updated. Inbetween the HttpGet and the HttpPost methods, the model becomes dissassociated from the database context. I think the following code work to re-attach it to the context so you can save the changes:

context.Entry(myEntity).State = EntityState.Modified;
context.MyEntities.Attach(myModel);
context.SaveChanges();

Edit: Forgot to add the re-attach method part, assuming this is an existing record in the DB that your updating.

Comments

0

This I find is a far more reliable means to adding items to the database:

// Note: db is your dbContext, model is your view model

var entity = db.Entry(model); // Required to attach the entity model to the database
                              //   you don't need the "var entity = " but its useful
                              //   when you want to access it for logging
db.Entry(model).State = EntityState.Added; // Required to set the entity as added - 
                                           //   modified does not work correctly

db.SaveChanges();             // Finally save

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.