0

I am trying to merge two DataTables - one representing current data, and one representing proposed insertions to that data.

These tables have the same schema, both with a simple, user-provided primary key string.

What I'd like is, if the a proposed insertion row has a key that is already present in the current data, an error should be thrown. However, the proposed addition just gets merged as a proposed alteration to the existing row, which is not what I want.

My current code is something along the lines of

currentData.EnforceConstraints = false;
currentData.Merge(additions);
currentData.EnforceConstraints = true;

where I'm actually merging whole DataSets, not just DataTables. I was hoping to get an error on the EnforceConstraints = true line, but I don't.

I also tried using diffgrams, but had the same problem - duplicate insertions get treated as modifications.

Is there a way to merge a set of insertions into a DataSet and have duplicate PKs be treated as an error rather than an update?


Similarly, since modified DataRows remember their original values, I'd hope that merging a modified row whose original values don't match the target row's current values would throw an exception too.

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • Did you check if your PK field is really as primary key field exposed to the datatable? – Kai May 03 '13 at 13:25
  • I just double-checked, and yes, it is the `PrimaryKey` for both the source and additions tables. (Maybe if I temporarily turned it off, it _wouldn't_ merge the values by key, then I could turn it back on and get an exception.) – Rawling May 03 '13 at 13:30
  • "Make sure the key is a primary key and not just a key. It took me hours before I noticed that I'd forgotten to tick the "Primary key" checkbox in the data-table designer. Once I checked it my merge worked perfectly." – Joe May 03 '13 at 15:05

1 Answers1

0

Isn't the Unique flag used for this purpose? My understanding is that for Merge it will merge rows based on Primary Key.