Scott
Scott

Reputation: 13941

Attaching an EF4 entity to a context more than one time

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.

Upvotes: 1

Views: 812

Answers (2)

w.brian
w.brian

Reputation: 17417

These issues are typically a result of how Entity Framework works when attaching objects. When you attach an entity, you are recursively attaching every entity that it references. The exceptions are misleading because we naturally assume the error is with the entity we're explicitly attaching, when in many cases the issue is with an entity that is being implicitly attached.

So, I'm not sure how abbreviated your code snippet is, but it seems like you may be attempting to attach an entity that is referencing a newly created entity.

Upvotes: 1

CodingGorilla
CodingGorilla

Reputation: 19872

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.

Upvotes: 2

Related Questions