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

brettrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.