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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:28:54+00:00 2026-06-05T11:28:54+00:00

So I’m working on an existing application that stores an entity in a Session

  • 0

So I’m working on an existing application that stores an entity in a Session variable and every postback attempts to re-attach it to a new context. I don’t particularly care for this implementation, but re-writing it is out of the question for now. The code looks something like this:

public partial class ReviewDetail
{
    DBContext context = new DBContext();

    public void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            Session["Review"] = context.Reviews.FirstOrDefault(x => x.ReviewID = id);
        }
        else
        {
            Review review = Session["Review"] as Review;
            context.Attach(review);
        }
    }
}

This works on both initial page load and on the FIRST postback. However, on subsequent postbacks, it fails with the following error:

An object with a temporary EntityKey value cannot be attached to an object context.

The strange thing (to me, anyways) is that when I step through the code in the VS debugger and examine the properties of review.EntityKey, the IsTemporary field is false.

Any suggestions on how I should approach a fix for this? I’ve tried calling .AttachTo(“Reviews”, review) but I end up with the same error.

What I think I know so far is that when I make the first .Attach call, it attaches the “review” object, but since it’s a reference object, it modifies the object in Session as well and adds an EntityKey to it (I may be incorrect on my terminology or my understanding here — this is a little bit of a weak area in EF for me). So now, after the first postback, my Session object has been “attached”. The second time around (when it fails), it’s already been attached, so it’s failing?

I think I’m on the right track, just not sure how to approach a fix from here. Ideally, we wouldn’t be doing it this way, but rewriting it to use a more correct approach probably isn’t going to happen here unless there are no other alternatives.

Any suggestions are appreciated.

EDIT: I should have mentioned this before someone answered with it, but I can’t go back to the database on each postback to get the Review. The object in session is modified later on and it’s data could be different from that of the data in the DB.

  • 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-06-05T11:28:55+00:00Added an answer on June 5, 2026 at 11:28 am

    I would take the entity from your session variable and it’s ID property to requery the context, like:

    public partial class ReviewDetail
    {
        DBContext context = new DBContext();
    
        public void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                Session["Review"] = context.Reviews.FirstOrDefault(x => x.ReviewID = id);
            }
            else
            {
                Review review = Session["Review"] as Review;
    
                Review updateReviewFromContext = context.ApplyCurrentValues<Review>("Reviews", review);         
            }
        }
    }
    

    It’s a simple change and makes it a bit irrelevant that you’re storing the entire object in the session, but if you can’t change that this should do the trick for you.

    UPDATE

    See: http://msdn.microsoft.com/en-us/library/dd487246

    The ApplyCurrentValues will take your updated Review from the session and apply its updated properties to the entity that is returned from the context. The only trick is you have to know what the EntitySetName is, in my example “Reviews”, but as long as that’s not going to change it can just be hard-coded.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT

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.