Linq to SQL, Ajax callbacks and page state in ASP.NET
This should be a frequent question, but not on SO yet, it seems so. I am using Linq for SQL for an application that allows various changes to the data in my model in memory, and the changes will then be queued for asynchronous processing. So, I'm not worried about updates (yet). Changes to different parts of the data can be made using modal pop-ups on the web page. Some are grid-based, others are just attributes. I am having problems with changes that cannot withstand postback cycles. Obviously, I don't want to store anything in the database until all changes are made and the user submits the page.
The route I've taken so far:
- I have disabled lazy loading.
- I am creating a [serializable] partial class for my root object
- I put this in session and retrieve it on onload event
This seems to work in principle. However, when I try to update some child properties after retrieving the object from the session
p.PhysicianSpecialties[0].physician_specialty_code = ddlSpecialty.SelectedItem.Value;
I am getting this error:
Operation is not valid due to the current state of the object.
The error was caused by calling this method in the generated Linq install method:
this.SendPropertyChanging();
Other properties update just fine:
p.PhysicianNames[0].first_name = txtFirstName.Text.ToUpper();
This does not cause an error.
My questions: Am I fundamentally wrong? Is there a better way to do this? What is causing the error?
Update: The exception is "System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException". I think I'm on to something , but don't know the solution yet.
source to share
I got the solution from MSDN. The problem is, and it is explained better in this thread than I can do it, that you cannot change the association unilaterally, but it needs to be done on both ends by assigning a new child, not just by changing the ID:
p.PhysicianSpecialties [0] .PhysicianSpecialtyLookup = db.PhysicianSpecialtyLookups.Single (c => (c.physician_specialty_code == ddlSpecialty.SelectedItem.Value));
source to share