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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:38:47+00:00 2026-05-24T18:38:47+00:00

I have two entities: Product and StockItem. When I insert a product – I

  • 0

I have two entities: Product and StockItem.
When I insert a product – I have to update the relevant StockItem.
When I delete a product – I have to update the relevant StockItem as well.

My problem is updating the stock item in case of product update (modification). In order to update the relevant StockItem, I need to know if a field named Group in the Product entity has changed. To do so I have to compare between the modified version of the entity and the unmodified version of the entity. But whenever I ask for the entity – the entity framework returns the attached entity (and not the old one from the database).

How can I ask the entity framework to return the old version / current db version of the entity (before change)?

  • 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-24T18:38:48+00:00Added an answer on May 24, 2026 at 6:38 pm

    What you see is result of identity map pattern used by ORM tools. Every entity with unique entity key can be attached to the context only once = you will never have two versions of the entity with the same entity key loaded twice to the same context.

    By default EF always returns already loaded instance if your query returns record with the same entity key. This behaviour can be changed but still you will have only single instance of the entity.

    Did you load the entity from EF with the same context instance as you are going to use for saving? If yes you already have old values. Use:

    ObjectStateEntry entry = objectContext.ObjectStateManager.GetObjectStateEntry(yourEntity);
    int someIntProperty = (int)entry.OriginalValues["SomeIntProperty"];
    

    If you didn’t load entity from the same context you can still use this approach but before that you must force EF to reload original values:

    objectContext.YourEntitySet.MergeOption = MergeOption.PreserveChanges;
    YourEntity entity = objectContext.YourEntitySet.Single(e => e.Id == entityId);
    

    Now the OriginalValues collection in entry will be populated from database.

    Another solution is simply using two different contexts – one for current entity state and one for old entity state.

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

Sidebar

Related Questions

I have two entities: product and tag connected by many-to-many relation. Here is a
I have two entities with a Unidirectional Many-to-One mapping. Here's Product : use Doctrine\Common\Collections\ArrayCollection;
I have two entities in 1:n relationship: Category and Product. public class Category {
Ok. So here is the deal. I have two entities - Product and Parts.
I have two entities: public class Product { [HiddenInput(DisplayValue=false)] public int ProductID { get;
I have two entities: products and bundles. Each one has its class. A product
I have two entities, a Shelf and a Product : public class Shelf {
I have two entities: Recipe and Ingredient. Entites: public class Ingredient { public int
I have two entities A and B. public class A{ @Id @GeneratedValue private Integer
I have two entities Visita and Cliente that implements respectively two interfaces IVisita and

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.