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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:46:14+00:00 2026-05-18T02:46:14+00:00

I’m currently inserting/updating fields like this (if there’s a better way, please say so

  • 0

I’m currently inserting/updating fields like this (if there’s a better way, please say so – we’re always learning)

public void UpdateChallengeAnswers(List<ChallengeAnswerInfo> model, Decimal field_id, Decimal loggedUserId)
{
    JK_ChallengeAnswers o;
    foreach (ChallengeAnswerInfo a in model)
    {
        o = this.FindChallengeAnswerById(a.ChallengeAnswerId);
        if (o == null) o = new JK_ChallengeAnswers();

        o.answer = FilterString(a.Answer);
        o.correct = a.Correct;
        o.link_text = "";
        o.link_url = "";
        o.position = FilterInt(a.Position);

        o.updated_user = loggedUserId;
        o.updated_date = DateTime.UtcNow;

        if (o.challenge_id == 0)
        {
            // New record
            o.challenge_id = field_id;  // FK
            o.created_user = loggedUserId;
            o.created_date = DateTime.UtcNow;

            db.JK_ChallengeAnswers.AddObject(o);
        }
        else
        {
            // Update record
            this.Save();
        }
    }

    this.Save(); // Commit changes
}

As you can see there is 2 times this.Save() (witch invokes db.SaveChanges();)

when Adding we place the new object into a Place Holder with the AddObject method, in other words, the new object is not committed right away and we can place as many objects we want.

But when it’s an update, I need to Save first before moving on to the next object, is there a method that I can use in order to, let’s say:

        if (o.challenge_id == 0)
        {
            // New record
            o.challenge_id = field_id;
            o.created_user = loggedUserId;
            o.created_date = DateTime.UtcNow;

            db.JK_ChallengeAnswers.AddObject(o);
        }
        else
        {
            // Update record
            db.JK_ChallengeAnswers.RetainObject(o);
        }
    }

    this.Save(); // Only save once when all objects are ready to commit
}

So if there are 5 updates, I don’t need to save into the database 5 times, but only once at the end.

Thank you.

  • 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-18T02:46:14+00:00Added an answer on May 18, 2026 at 2:46 am

    Well if you have an object which is attached to the graph, if you modify values of this object, then the entity is marked as Modified.

    If you simply do .AddObject, then the entity is marked as Added.

    Nothing has happened yet – only staging of the graph.

    Then, when you execute SaveChanges(), EF will translate the entries in the OSM to relevant store queries.

    Your code looks a bit strange. Have you debugged through (and ran a SQL trace) to see what is actually getting executed? Because i can’t see why you need that first .Save, because inline with my above points, since your modifying the entities in the first few lines of the method, an UPDATE statement will most likely always get executed, regardless of the ID.

    I suggest you refactor your code to handle new/modified in seperate method. (ideally via a Repository)

    • 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.