diff --git a/Src/Newtonsoft.Json/Converters/DataSetConverter.cs b/Src/Newtonsoft.Json/Converters/DataSetConverter.cs index 214728e67..a6843880e 100644 --- a/Src/Newtonsoft.Json/Converters/DataSetConverter.cs +++ b/Src/Newtonsoft.Json/Converters/DataSetConverter.cs @@ -85,7 +85,8 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer DataSet ds = (objectType == typeof(DataSet)) ? new DataSet() : (DataSet)Activator.CreateInstance(objectType)!; - + ds.EnforceConstraints = false; // necessary in case the typed dataset contains foreign key constraints and the tables were serialized out of order + DataTableConverter converter = new DataTableConverter(); reader.ReadAndAssert(); @@ -105,6 +106,9 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer reader.ReadAndAssert(); } + // once all tables are populated there should be no foreign key constraint violations so reset EnforceConstraints + ds.EnforceConstraints = true; + return ds; } @@ -122,4 +126,4 @@ public override bool CanConvert(Type valueType) } } -#endif \ No newline at end of file +#endif