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 8205289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:10:47+00:00 2026-06-07T08:10:47+00:00

I am working on a project which allows the user to edit a list

  • 0

I am working on a project which allows the user to edit a list of entities. I map these entities to view models and display them with editor fields. When the user presses the submit button, I go through each model and update it like so:

foreach (var viewModel in viewModels)
{
    //Find the database model and set the value and update
    var entity = unit.EntityRepository.GetByID(fieldModel.ID);
    entity.Value = viewModel.Value;
    unit.EntityRepository.Update(entity);
}

The above code works, however as you can see we need to hit the database twice for every entity (once to retrieve and another to update). Is there a more efficient way of doing this using Entity Framework? I noticed that each update generates a separate SQL statement. Is there a way of committing all the updates after the loop has finished?

  • 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-07T08:10:51+00:00Added an answer on June 7, 2026 at 8:10 am

    Here are two ways I know of to update an entity in the database without doing a retrieval of the entity first:

    //Assuming person is detached from the context
    //for both examples
    public class Person
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public DateTime BornOn { get; set; }   
    }
    
    public void UpdatePerson(Person person)
    {
      this.Context.Persons.Attach(person)
      DbEntityEntry<Person> entry = Context.Entry(person);
      entry.State = System.Data.EntityState.Modified;
      Context.SaveChanges();
    }
    

    Should yield:

    Update [schema].[table]
    Set Name = @p__linq__0, BornOn = @p__linq__1
    Where id = @p__linq__2
    

    Or you can just specify fields if you need to (probably good for tables with a ton of columns, or for security purposes, allows only specific columns to be updated:

    public void UpdatePersonNameOnly(Person person)
    {
      this.Context.Persons.Attach(person)
      DbEntityEntry<Person> entry = Context.Entry(person);
      entry.Property(e => e.Name).IsModified = true;
      Context.SaveChanges();
    }
    

    Should yield:

    Update [schema].[table]
    Set Name = @p__linq__0
    Where id = @p__linq__1
    

    Doesn’t the .Attach() go to the database to retrieve the record first and then merges your changes with it ? so you end up with roundtrip anyway

    No. We can test this

    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.ComponentModel.DataAnnotations;
    
    public class Program
    {
        public static void Main()
        {
    
            var movie1 = new Movie { Id = 1, Title = "Godzilla" };
            var movie2 = new Movie { Id = 2, Title = "Iron Man" };
            using (var context = new MovieDb())
            {
                /*
                context.Database.Log = (s) => {
                    Console.WriteLine(s);
                };
                */
    
                Console.WriteLine("========= Start Add: movie1 ==============");
                context.Movies.Add(movie1);
                context.SaveChanges();
                Console.WriteLine("========= END Add: movie1 ==============");
    
                // LET EF CREATE ALL THE SCHEMAS AND STUFF THEN WE CAN TEST
    
                context.Database.Log = (s) => {
                    Console.WriteLine(s);
                };
    
                Console.WriteLine("========= Start SELECT FIRST movie ==============");
                var movie1a = context.Movies.First();
                Console.WriteLine("========= End SELECT FIRST movie ==============");
    
                Console.WriteLine("========= Start Attach Movie2 ==============");
                context.Movies.Attach(movie2);
                Console.WriteLine("========= End Attach Movie2 ==============");
    
                Console.WriteLine("========= Start SELECT Movie2 ==============");
                var movie2a = context.Movies.FirstOrDefault(m => m.Id == 2);
                Console.WriteLine("========= End SELECT Movie2 ==============");
                Console.Write("Movie2a.Id = ");
                Console.WriteLine(movie2a == null ? "null" : movie2a.Id.ToString());
            }
        }
    
        public class MovieDb : DbContext
        {
            public MovieDb() : base(FiddleHelper.GetConnectionStringSqlServer()) {}
            public DbSet<Movie> Movies { get; set; }
        }
    
        public class Movie
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int Id { get; set; }
    
            public string Title { get; set; }
        }
    }
    

    If attach makes any DB calls, we will see them between the Start Attach Movie2 and End Attach Movie2. We also verify that the documentation that states:

    Remarks

    Attach is used to repopulate a context with an entity that is known to already exist in the database.

    SaveChanges will therefore not attempt to insert an attached entity into the database because it is assumed to already be there.

    After attaching the movie2, we can attempt to select it from the DB. It should not be there (because EF only assumes it is there).

    ========= Start Add: movie1 ==============

    ========= END Add: movie1 ==============

    ========= Start SELECT FIRST movie ==============

    Opened connection at 1/15/2020 5:29:23 PM +00:00

    SELECT TOP (1)

    [c].[Id] AS [Id],

    [c].[Title] AS [Title]

    FROM [dbo].[Movies] AS [c]

    — Executing at 1/15/2020 5:29:23 PM +00:00

    — Completed in 23 ms with result: SqlDataReader

    Closed connection at 1/15/2020 5:29:23 PM +00:00

    ========= End SELECT FIRST movie ==============

    ========= Start Attach Movie2 ==============

    ========= End Attach Movie2 ==============

    ========= Start SELECT Movie2 ==============

    Opened connection at 1/15/2020 5:29:23 PM +00:00

    SELECT TOP (1)

    [Extent1].[Id] AS [Id],

    [Extent1].[Title] AS [Title]

    FROM [dbo].[Movies] AS [Extent1]

    WHERE 2 = [Extent1].[Id]

    — Executing at 1/15/2020 5:29:23 PM +00:00

    — Completed in 2 ms with result: SqlDataReader

    Closed connection at 1/15/2020 5:29:23 PM +00:00

    ========= End SELECT Movie2 ==============

    Movie2a.Id = null

    So no SQL called during the attach, no error message attaching it, and it’s not in the database.

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

Sidebar

Related Questions

I'm working on a project which allows the advanced user to define their own
I'm working on a project which uses user authentication. I'm facing a issue with
Working on an ASP.NET 4.0 project, which uses user controls to dynamically generate a
I am working on a .NET project, which needs to interact with some user
I am working on an ASP.NET MVC project which allows users to construct arbitrarily
I have a project coming up to build an interface which allows a user
Iam working on a project which involves writing a Mork File (Mork is a
I'm working on a project which will deliver small pieces of text to a
I'm working on a project which includes some slightly more complex dynamic layout of
I am working on a project which involves uploading flash video files to a

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.