I am still facing some problems when using LINQ-to-SQL.
I am also looking for answers by myself, but this problem is so akward that I am having problems to find the right keywords to look for it.
I have this code here:
public CustomTask SaveTask(string token, CustomTask task)
{
TrackingDataContext dataConext = new TrackingDataContext();
//Check the token for security
if (SessionTokenBase.Instance.ExistsToken(Convert.ToInt32(token)) == null) return null;
//Populates the Task - the "real" Linq to SQL object
Task t = new Task();
t.Title = task.Title;
t.Description = task.Description;
//****The next 4 lines are important****
if (task.Severity != null)
t.Severity = task.Severity;
else
t.SeverityID = task.SeverityID;
t.StateID = task.StateID;
if (task.TeamMember != null)
t.TeamMember = task.TeamMember;
else
t.ReporterID = task.ReporterID;
if (task.ReporterTeam != null)
t.Team = task.ReporterTeam;
else
t.ReporterTeamID = task.ReporterTeamID;
//Saves/Updates the task
dataConext.Tasks.InsertOnSubmit(t);
dataConext.SubmitChanges();
task.ID = t.ID;
return task;
}
The problem is that I am sending the the severity, and then, when I get this situation:
DB State before calling the method:
ID Name
1 high
2 medium
3 low
Call the method selecting “medium” as severity
DB State after calling the method:
ID Name
1 high
2 medium
3 low
4 medium
The point is:
-Why is it duplicating this entry??
Some explanation about the code:
CustomTask is almost the same as Task, but I was having problems regarding serialization as can be seen here
Edit: just found out that the code is sending an Severity object and not the ID, I am going to change it. But it is still very strange that it duplicates the entry 🙁
Edit2: Updated the question due to new information
The problems was that the services are Rest, so a new DataContext is created every call, generating the duplicated entry.
Using the ID avoids this problem.