1

I am busy adding database (SQLite) support to my latest application. Since there are some tables that has rows that reference rows in other tables I thought it might be a good idea to start using foreign key constraints. A few google searches later I was able to apply the constraints to one table as a test run.

var c = workingDataSet.Tables["measurements"].Columns["reference_realestate"];
var p = workingDataSet.Tables["realestates"].Columns["id"];

ForeignKeyConstraint fkcon = new ForeignKeyConstraint(p, c);
fkcon.DeleteRule = Rule.None;                

workingDataSet.Tables["measurements"].Constraints.Add(fkcon);

This works, if I try to remove a "realestates" record that is referenced in "measurements" the following exception is raised:

'An unhandled exception of type 'System.Data.InvalidConstraintException' occurred in System.Data.dll Additional information: Cannot delete this row because constraints are enforced on relation Constraint1, and deleting this row will strand child rows.'

My problem now is where and how do I handle this exception? After reading some other posts I found this which points me to handle the exception in the DataGrid control. I have tried using RowValidationRules but it doesn't seem to work either.

<DataGrid.RowValidationRules>
    <local:RowValidation_Measurements ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>

Any ideas how I can achieve this? Oh, and I have to mention I am using MVVM.

Avalan
  • 159
  • 1
  • 10
  • Could this be the bug referred to [here](http://wpf.codeplex.com/discussions/39077). If this is the case surely it should be resolved by now? – Avalan May 26 '15 at 08:08
  • I think I might start to get a better understanding of why I have trouble catching this exception. It appears that the exception is thrown inside an event handler of the DataSet while DataGrid will only catch exceptions raised in the setter of the property to which it is bound. – Avalan May 27 '15 at 08:32

0 Answers0