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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:34:17+00:00 2026-05-23T13:34:17+00:00

Let’s say I have this code: class Score { public Update(int score) { update

  • 0

Let’s say I have this code:

class Score
{
     public Update(int score)
     {
         update score but do not call (context.SaveChanges())
     }
}

 class Foo
 {
     public DoSomething(int update)
     {
           Score score = new Score();
           score.Update(2);
           SomeObj obj = (select object);
           obj.Soo = 3;
           context.SaveChanges();
      }
 }

Basically to make it work, I need to explicity provide SaveChanges in method Update. But when I have 4 such methods in row, and 34243 users want to update data, I don’t think saving for each one in 4 trips would be a good idea.

Is there way in EF4.1 to delay database update the last moment, in provided example, Or I’m forced to explicity save for each method ?

EDIT:
For clarification. I tried to do not call SaveChanges in external method, and only one time where the changes mu be saved.

I will give an real example:

public class ScoreService : IScoreService
{
private JamiContext _ctx;
    private IRepository<User> _usrRepo;
    public ScoreService(IRepository<User> usrRepo)
    {
        _ctx = new JamiContext();
        _usrRepo = usrRepo;
    }

    public void PostScore(int userId, GlobalSettings gs, string name)
    {
        User user = _ctx.UserSet.Where(x => x.Id == userId).FirstOrDefault();
        if (name == "up")
        {
            user.Rating = user.Rating + gs.ScoreForLike;
        }
        else if (name == "down")
        {
            user.Rating = user.Rating - Math.Abs(gs.ScoreForDislike);
        }
    }
 }

And Now:

public PostRating LikeDislike(User user, int postId, int userId, GlobalSettings set, string name)
    {
        PostRating model = new PostRating();
        var post = (from p in _ctx.PostSet
                    where p.Id == postId
                    select p).FirstOrDefault();
        if (name == "up")
        {
            post.Like = post.Like + 1;
            model.Rating = post.Like - post.Dislike;
        }
        else if (name == "down")
        {
            post.Dislike = post.Dislike + 1;
            model.Rating = post.Like - post.Dislike;
        }

        PostVote pv = new PostVote();
        pv.PostId = post.Id;
        pv.UserId = user.Id;
        _ctx.PostVoteSet.Add(pv);
        _scoreSrv.PostScore(userId, set, name);
        _ctx.SaveChanges();

        return model;
   }

I this case user rating do not update, Until I call SaveChanges in PostScore

  • 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-23T13:34:18+00:00Added an answer on May 23, 2026 at 1:34 pm

    In your example it looks like PostScore and LikeDislike use different context instances. That is the source of your problem and there is no way to avoid calling multiple SaveChanges in that case. The whole operation is single unit of work and because of that it should use single context instance. Using multiple context instances in this case is wrong design.

    Anyway even if you call single SaveChanges you will still have separate roundtrip to the database for each updated, inserted or deleted entity because EF doesn’t support command batching.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have an int that represents the number of rows in a
Let's say I have the following, declare @A table (a int) insert into @A
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let’s say I have a data grid and there’s a method attached to clicking
Let's say, for the sake of argument, that I have 30 asp.net web applications

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.