How do I know which table prevents row deletion from Entity Framework 6?

I use EF 6 in my project when I want to remove a row from the it it trow exception because this row is referenced in another table. I want to know which table is stopping me from deleting C # and EF code.

enter image description here

We have an entry in table A, and this row is not listed in table C in table C. Is it possible for EF to know that table C does not allow me to delete this row?

I also use Sql-Server 2012.

+5
source share
1 answer

If you are trying to dynamically sort this at runtime to show the user or determine before attempting to delete, and you are not sure of a possible conflict, you can use sys tables and some dynamic sql to sort it.

  • Use sys.objects to find your table name and get id_id.
  • Use sys.foreign_keys to find tables that reference your table.
  • Use sys. foreign_key_columns to get the exact column numbers.
  • Use sys.columns to get the corresponding column names.
  • Create dynamic SQL to look up table and column names and find offensive rows using values ​​from the original row that cannot be deleted.
  • List the returned tables in sys.objects . Optionally list the number of rows.
  • It is not necessary to use dynamic SQL to create other statements (for example, to delete offensive records - this may be required recursively - take care of this - you can lose a lot of data! )
0
source

All Articles