• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1525
  • Last Modified:

Why can't LINQ delete this?

In .net 3.5, I have this relationship in my database:

Interactions (1) ---- (many)QuestionResponses

In the code below, everything works fine if I leave off the last two lines (delete interaction).  Once I add the last two lines, I get this error:
System.Data.Linq.ChangeConflictException : Row not found or changed.
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()

I understand the error can occur if the row was marked for deletion and then not found.  That isn't the case here.  I delete four rows from QuestionResponses then delete one from Interactions.   So the row in Interactions is there.  Any suggestions?
var interaction = (myquery).SingleOrDefault();
if (interaction != null)
{
  var Question = context.QuestionResponses.Where(x => x.InteractionID == interaction.InteractionID).ToList();
  context.QuestionResponses.DeleteAllOnSubmit(Question);
}
  context.SubmitChanges();
  context.Interactions.DeleteOnSubmit(interaction);
  context.SubmitChanges(); //fail here with error

Open in new window

0
brettr
Asked:
brettr
  • 2
1 Solution
 
Fernando SotoCommented:
Hi brettr;
 
If interaction is null what happens here

  context.Interactions.DeleteOnSubmit(interaction);
  context.SubmitChanges(); //fail here with error

Their is nothing to delete?

Fernando
0
 
brettrAuthor Commented:
I had that delete code of interaction in a null check but moved out temporarily while I'm trying to figure out what's wrong.  interaction isn't null in this case.
0
 
PockyMasterCommented:
why are you submitting twice?
try removing the first submitchanges.

the QuestionResponses in the interaction object might have changed since you submitted the first time
0
 
PockyMasterCommented:
In addition:
wouldn't the could below be enough?
var interaction = (myquery).SingleOrDefault();
if (interaction != null)
{
   context.QuestionResponses.DeleteAllOnSubmit(interaction.QuestionResponses);
 context.Interactions.DeleteOnSubmit(interaction);
context.SubmitChanges();
}

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now