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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:46:15+00:00 2026-05-16T06:46:15+00:00

I’m looking at an example of a save method in a Products repository from

  • 0

I’m looking at an example of a save method in a Products repository from Steven Sanderson’s book, Pro ASP.NET MVC 2 Framework:

public void SaveProduct(Product product)
{
    // if new product, attach to DataContext:
    if (product.ProductID == 0)
        productsTable.InsertOnSubmit(product);
    else if (productsTable.GetOriginalEntityState(product) == null)
    { 
        // we're updating existing product
        productsTable.Attach(product);
        productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
    }
    productsTable.Context.SubmitChanges();            
}

I do not understand the logic in the else if line:

else if (productsTable.GetOriginalEntityState(product) == null)

As I understand it, GetOriginalEntityState() returns the original state of the specified entity.. in this case that entity is product.

So this else if statement reads to me like: “If an original doesn’t exist then…” but that doesn’t make sense because the book is saying that this checks that we are modifying a record that already DOES exist.

How should I understand GetOriginalEntityState in this context?

Edit

By the way, this excerpt came from chapter 6, page 191… just in case anyone has the book and wants to look it up. The book just has that function in the code sample but it never explains what the function does.

  • 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-16T06:46:16+00:00Added an answer on May 16, 2026 at 6:46 am

    This is a little bit of a guess since I have never actually used GetOriginalEntityState but the question peaked my interest to figure out what is going on.

    I think the intent here is to check that product is still attached to the original DataContext

    The line:

    if (productsTable.GetOriginalEntityState(product) == null) 
    

    I think this will return null if product has been dettached or created manually and not handled by the DataContext.

    From MSDN:

    This method returns the original state
    of an entity since it was either
    created or attached to the current
    DataContext. The original state of an
    entity that has been serialized and
    deserialized must be provided by an
    independent tracking mechanism and
    supplied when the entity is attached
    to a new DataContext. For more
    information, see Data Retrieval and
    CUD Operations in N-Tier Applications
    (LINQ to SQL).

    I think the key line to understand is:

    This method returns the original state
    of an entity since it was either
    created or attached to the current
    DataContext.

    GetOriginalEntityState is used so that the method can receive an object with the option of not being attached already to the DataContext. Attached meaning, returned by a Linq To SQL call vs just creating an instance like Product p = new Product() { ... };. If it is not attached, it will attach it to the DataContext and keep any of the values that were modified (preserving the update values) due to the RefreshMode.KeepCurrentValues param.

    Then the productsTable.Context.SubmitChanges(); always happens since even if it is dettached, the GetOriginalEntityState will make sure it gets attached so the submit will work.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.