Reputation: 3854
I have a code:
MyDataClassesDataContext dc = new MyDataClassesDataContext();
Table<FormsAuthorisation> databaseAuthorisation = GetFormsAuthorisation();
Table<ADForm> databaseForms = GetADForm();
foreach (var auth in authorisation)
{
var databaseAuth = databaseAuthorisation.Where(p => p.GroupID == auth.GroupID && p.FormID == auth.FormID).FirstOrDefault();
databaseAuth.CanRead = auth.CanRead;
databaseAuth.CanWrite = auth.CanWrite;
dc.SubmitChanges();
}
foreach (var form in forms)
{
var databaseForm = databaseForms.Where(p => p.FormID == form.FormID).FirstOrDefault();
databaseForm.FormDescription = form.FormDescription;
dc.SubmitChanges();
}
dc.SubmitChanges();
This code is not saving any of the updated value at all.
I have googled it for long but there are not good results
All my tables have primary key defined.
Upvotes: 0
Views: 486
Reputation: 48415
I would say it is quite simply because you are pulling databaseAuthorisation
from a different data context (via GetFormsAuthorisation
method). You are not actually making any changes to dc
. Therefore, when you submit the changes, the context has no changes to make.
The same also applies to databaseForms
.
Options are you can overload your methods to allow for the dc
data context to be passed in and used. Or you can replicate the functionality of you methods inside this block of code. Obviously, replicating is not ideal if you methods apply any reasonable amount of logic.
For example:
public Table<FormsAuthorisation> GetFormsAuthorisation()
{
MyDataClassesDataContext dc = new MyDataClassesDataContext();
return GetFormsAuthorisation(dc);
}
public Table<FormsAuthorisation> GetFormsAuthorisation(MyDataClassesDataContext dc)
{
//do whatever you already do inside your GetFormsAuthorisation function using dc parameter
}
Now, you can still use the function without the param for a read-only execution (e.g. Viewing a record) and you can use the overloaded version to allow modification of records:
MyDataClassesDataContext dc = new MyDataClassesDataContext();
Table<FormsAuthorisation> databaseAuthorisation = GetFormsAuthorisation(dc);
//make any changes
dc.SubmitChanges();
Upvotes: 2