I have two tables with a foreign key constraint how can I delete rows from both of them in one transaction? linq to SQL seems to call my deletes in the wrong order. Is there somewhere I can check and make sure the constraint is recognized properly by linq to SQL??
DataContext.OtherImages.DeleteOnS开发者_如何学JAVAubmit(myOtherImage);
DataContext.Images.DeleteOnSubmit(myImage);
DataContext.SubmitChanges();
The Foreign key constraint is on OtherImages. Thanks!
You should have something like
[Association(
Name="FK_OtherImages_Images",
Storage="_OtherImages",
OtherKey="ImageId",
DeleteRule="NO ACTION")]
public EntitySet<OtherImage> OtherImages{
...
}
in your Image class.
DataContext.OtherImages.DeleteOnSubmit(myOtherImage);
DataContext.Images.DeleteOnSubmit(myImage);
DataContext.SubmitChanges();
should work fine. My guess is that you're forgetting about another foreign key. You can see what query is being run by doing
DataContext.Log = Console.Out;
or something equivalent. I wouldn't recommend cascading the deletes just to make this work since it should work without doing that.
simple logic
set the foreign key relationship
go to option :: Table designer
- Enfore for replication : NO
- Enfore foreign key constrain : NO
精彩评论