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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:11:38+00:00 2026-05-25T19:11:38+00:00

I am having problems saving database records using Linq in visual studio 2010 and

  • 0

I am having problems saving database records using Linq in visual studio 2010 and sql server 2008.
My problem is that when I am editing some records I sometimes check the original database record for validation purposes, only the original entry seems to be updated in real time – I.e. it is already exactly the same as the edited record, before I have submitted the changes!

Could anyone suggest an effective method of coping with this? I have tried using a 2nd database connection or a 2nd data repository to call the original record from the db but it appears to be already changed when I debug it.

public void SaveobjectEdit(object objectToEdit)
{ 
    object originalObject = GetobjectById(objectToEdit.Id); 
    if (originalObject.objectStatus !=  objectToEdit.objectStatus) 
    { 
        originalObject.objectStatus = objectToEdit.objectStatus; 
    }
SaveChanges(); 
}

The save changes just calls _db.SubmitChanges(); by the way


Has no one got any ideas for the above question?

I hope I was clear – for validation purposes I would like to compare an original database record with one that I am editing. The problem is that when I edit a record and then attempt to retrieve the original record before saving – the original record is exactly the same as the edited record.

  • 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-25T19:11:39+00:00Added an answer on May 25, 2026 at 7:11 pm

    If you’re trying to retrieve the original record in code, from the same ‘context’ using the same access method, then it will contain the updated object. Rather than ask why you’re doing this or what you’re trying to achieve, I’ll instead explain how I understand the data context / object context to work (in a very loose and vague fashion).

    The context is something like an in-memory representation of your database, where everything is lazy-loaded. When you instantiate the context you’re given an object which represents your data model (of course it may not be a 1-1 representation, and can contain various abstractions). Nothing is loaded into the context until necessary; any queries you write stay as queries until you peer in their results. When you access an item (e.g. GetobjectById(objectToEdit.Id)) the item is loaded into the context from the database and you can get and set its properties at your leisure.

    Now, the important part: When you access an item, if it has already been loaded into the context then that in-memory object is returned. The context doesn’t care about checking changes made; the changes won’t be persisted to the database until you submit, but they remain in memory.

    The way to refresh the in-memory objects is to call the Refresh method on the context. Try this test:

    using (var db = new MyObjectContext())
    {
        var item = db.Items.First();
    
        item.Name = "testing this thing";
        Console.WriteLine(db.Shifts.First().Name);
    
        db.Refresh(System.Data.Objects.RefreshMode.StoreWins, db.Items);
        Console.WriteLine(db.Shifts.First().Name);
    }
    

    I believe this pattern makes a lot of sense and I’m not sure it could work any other way. Consider this:

    foreach (var item in db.Items)
    {
        item.Name = "test";
    }
    
    Assert(db.Items.All(item => item.Name == "test"));
    

    Would you want the Assert to fail? Should those items be reloaded? I don’t believe so. I’m looking at the items in my context, not in the database. I’m not checking whether items in the database have been updated, but instead that I’ve updated all the items in the context of my code.

    This is a good reason why I don’t use MyObjectContext db – it is not a ‘db’ or a database connection. It’s a context within which I can change whatever I want, so I name it such: MyObjectContext context.

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

Sidebar

Related Questions

Im having problems building a query with the linq to sql data query expression
I'm having problems using textures that are larger than the OpenGL window or the
I am having problem with saving the changes to database. I am updating the
I have having a problem saving the sign up form using Devise. I am
Im having problems displaying records to my view when passing viewdata to a user
I am having problems manually looping through xml data that is received via an
I am having problems converting a string in the format yyyy-MM-ddZ using VB.net. For
I am having a problem using PHP's PDO object to prepare an update statement
I'm haing a few problems updating a row in my database using Linq2Sql. Inside
I'm working on ASP.NET 4.0 and I'm having problems for saving the selected value

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.