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

  • Home
  • SEARCH
  • 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 3940412
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:24:56+00:00 2026-05-20T00:24:56+00:00

I’m working on an MVC site with Entity Framework Code First, using Ninject for

  • 0

I’m working on an MVC site with Entity Framework Code First, using Ninject for DI to the controllers, and I’ve run into a design question. I’ve seen two methods used for updates with Code First. The first uses a “get by id,” changes the values of the object returned, then calls Context.SaveChanges(), like this:

Controller:

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
    //Create a vm to hold the data from the form.
    var sectionVm = new SectionEditViewModel();
    //Copy the data from the http header to the object.
    UpdateModel(sectionVm);
    //Get the object from the database.
    //_sectionRepository is injected in to the constructor.
    var section = _sectionRepository.GetById(sectionVm.SectionId);  
    //Map from the view model to the actual model.
    var viewSection = Mapper.Map(sectionVm, section);
    _sectionRepository.Update();

    return RedirectToAction("Index");
}

Repository:

    public void Update()
    {
        _context.SaveChanges();
    }

The second method creates the model object, attaches it to the context, changes the state of the object, then calls SaveChanges(). Illustrated here with a test method as the consumer:
Test:

    [TestMethod]
    public void CanUpdateSection()
    {
        var repo = new SectionRepository();
        var testSection = GetMockSection();
        testSection.SomeProperty = "A new value";
        testContact.AnotherProperty = "Another new value";
        repo.Update(testSection);
        var persistedUpdatedSection = repo.GetById(testSection.Id);
        Assert.IsNotNull(persistedUpdatedSection);
        CompareSections(testSection, persistedUpdatedSection);
    }

Repository:

    public void Update(Section entity)
    {
        using(var context = new SectionContext())
        {
            context.Sections.Attach(entity);
            context.Entry(entity).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }
    }

Which way is preferable, or is there another, better way?

  • 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-05-20T00:24:57+00:00Added an answer on May 20, 2026 at 12:24 am

    The first way is better. If you keep the SaveChanges() method in it’s own Repository method, you can make many edits and then attempt to commit them all at once using Update(). If there is an issue with one edit, all your changes will be rolled back when Update() is called.

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

Sidebar

Related Questions

No related questions found

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.