Situation: Object in session from a past context can't be set as another object's parent since other object is in a new context.
Say I have a User in session that I had retrieved from a context. Now the page reloads, that context has been dismissed, and a new context is made.
someUser = context.First(user => user.id == id);
Session["SomeUser"] = someUser
...
context.Dispose();
Page Reload
userAddress = new UserAddress();
userAddress.User = (User)Session["SomeUser"]; //BOOM NOT IN SAME CONTEXT
What I would like to do is:
if(!context.SomeUsers.Contains((User)Session["SomeUSer"]) //Only check context NOT DATABASE
{
//Reload from database
//Set user in session to new object
}
The idea is that if the object in session does not belong to the current context, reload it from the database so that it is now "owned" by the same context as every other object in the current context. (I am using a context per request)
UPDATE
So I did this temporarily until I can get a better idea of how to fix this:
Int32 sessionUser = sessionUser .UserID;
var userCheck = EntityContext.Context.ChatUsers.First(item => item.UserID == returnValueID);
if (userCheck != sessionUser)
{
sessionUser = userCheck;
}
The idea is to see if the object in session (sessionUser) is the same as the one "in" the context. Now the if works just fine. First time the context is created, that SHOULD hit the database and grab the user. Once compared, it's obvious they are not the same and the sesionUser is now the user in the context. Next time that if is checked, the sessionUser and the userToCheck are the same.
Problem still is the :
var userCheck = EntityContext.Context.ChatUsers.First(item => item.UserID == returnValueID);
ALWAYS hits the database. This is not a good solution.
More Update
Meh this may be the answer after all. I had forgotten this rule:
x is an property of type ObjectQuery. When you execute an ObjectQuery, it will always hit the backing store. That’s what they do. If you don’t want to execute a database query, then don’t use an ObjectQuery.