Reputation: 5550
I have a Database with the tables Users
and Uploads
. The important columns are:
Users -> UserID
Uploads -> UploadID, UserID
The primary key in the relationship is Users -> UserID
and the foreign key is Uploads -> UserID
.
In LINQ to SQL
, I do the following operations:
Retrieve files
var upload = new Upload();
upload.UserID = user.UserID;
upload.UploadID = XXX;
db.Uploads.InsertOnSubmit(upload)
db.SubmitChanges();
If I do that and rerun the application (and the db
object is re-built, of course) - if do something like this:
foreach(var upload in user.Uploads)
I get all the uploads with that user's ID. (like added in the previous example)
The problem is, that my application, after adding an upload an submitting changes, doesn't update the user.Uploads
collection. i.e - I don't get the newly added uploads.
The user
object is stored in the Session
object. At first, I though that the LINQ to SQL Framework
doesn't update the reference of the object, therefore I should simply "reset" the user
object from a new SQL
request. I mean this:
Session["user"] = db.Users.Where(u => u.UserID == user.UserID).SingleOrDefault();
(Where user
is the previous user)
But it didn't help.
Please note: After rerunning the application, user.Uploads
does have the new upload!
Did anyone experience this type of problem, or is it normal behavior? I am a newbie to this framework. I would gladly take any advice.
Thank you!
P.S I have a DB.Uploads.Count()
line in another method, and it does increase after the addition of the upload.
Edit: Some things I have tried:
db = new new DBDataContext();
.db.Dispose; db = new DBDataContext();
after db.SubmitChanges();
in my first method, just to see if it does anything. It didn't.user.Uploads.Add(upload);
. It does add the appropriate object to user.Uploads
which is then used in the foreach
loop. Well, duh. But I am still waiting for some explanation about this behavior. Thank you!Upvotes: 0
Views: 1054
Reputation: 376
Storing the User object in the Session causes it to be disconnected from the data context on subsequent requests. Thus, any changes made to the database (for the user entity or any of its child entities) will not be reflected in the object instance.
To deal this this, you have several options. As you have done, you could simply update the Session-stored object instance yourself any time you make changes. You could also reconnect the entity stored in the Session before making any database changes (which in itself can have issues). You could also refresh the session object instance any time you change the data. And finally, you could not store the object in the session at all, and just retrieve it from the database any time you need it.
You mention that you tried refreshing the stored session object and it didn't help. I'm assuming you did this after calling SubmitChanges()
, correct? This should have worked. Could you post the two methods in question (ie. the full method inserting the new upload and the method containing the foreach
loop) and how they interact, please?
Upvotes: 1