开发者

Foreign Key constraint issue when deleting from multiple tables using linq to sql

开发者 https://www.devze.com 2023-02-13 11:55 出处:网络
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 ma

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

  1. Enfore for replication : NO
  2. Enfore foreign key constrain : NO
0

精彩评论

暂无评论...
验证码 换一张
取 消