开发者

Is there any way to show records that are preventing a delete due to referencial integrity?

i have a generic problem all over my application and want to see if there is a common generic way of solving it.

The main issue is how to let users know which records are linked when you try to delete a record from a joined table.

for example, let's say i have two database tables.

  1. Customer
  2. Branch

where Customer has a branchId field.

if i go delete a record from the branch table, it will fail if there are customers that have that branchId in its foreign key.

if i try to delete this through code, i get an error sayin开发者_运维问答g that it can't delete because of a referencial integrity constraint but it doesn't let me know what the actual linked tables is or which particular records are causing the link.

Is there any generic C# code that handles this situation that would either:

  1. Give a better error message back and show you the records that are links
  2. Even better, lets you change the linked records at that time to a default reference or a null reference (in cases where the foreign key is nullable)


You might consider creating a trigger on the Branch table that sets any Customer records referencing the Branch record that is going to be deleted to BranchId = null.

This should allow you to delete Branches and remove any associations from existing customers.

Here is an article that goes over something similar.

Be warned that you will probably have some confused customers if you start changing their data without them knowing it...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜