Re: How to deal with cascading changes?
From: Patrick (patrick_at_nospam.com)
Date: 09/21/04
- Next message: Kevin Yu [MSFT]: "RE: Why textboxes don't go blank when I click addnew"
- Previous message: Kevin Yu [MSFT]: "RE: Why textboxes don't go blank when I click addnew"
- In reply to: Marina: "Re: How to deal with cascading changes?"
- Next in thread: William \(Bill\) Vaughn: "Re: How to deal with cascading changes?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 21 Sep 2004 09:41:10 +0200
Marina, thanks for your reply.
Disabling constraints does not solve my problem as the problem does not lie
in the dataset.
The problem is this:
I have two tables: Parent and Child. In SQL Server table Parent has a
primary key with ON UPDATE CASCADE.
This primary key is also part of the primary key of table Child.
I have a dataset with also these two tables. I now change the key of Parent.
In the dataset this change will also be cascaded to table Child.
I have two data adapters, one for Parent and one for Child, which I use for
updating the database.
Suppose I call the Update method on the data adapter for the Parent first:
- In the database the primary key will cascade to table Child.
- When calling the data adapter Update method for Child, the keys in the
database will no longer match those of the DataRowVersion.Original version
in the dataset (which are used in the WHERE clause of the UPDATE command).
Therefore the Update method fails as it can no longer find the appropriate
Child rows.
Suppose I call the Update method on the data adapter for the Child first:
- The UPDATE command will try to set the FK of the Child table to a value
that is not yet set in the Parent table. Therefore I get a constraint
violation.
I have found the following workaround for my problem (can't call it a
solution):
- I change the primary key in table Parent in the dataset
- I then update table Parent in the database (not table Child)
- I then call AcceptChanges() on the dataset. This to let the dataset
"synchronize" with the actual values in the database.
- Then I can go about with the changes on other columns.
This workaround costs me an extra round-trip to the database. I hope someone
out there has a better solution as I feel my problem must be a very common
pattern.
Patrick
"Marina" <someone@nospam.com> wrote in message
news:uKfqERxnEHA.1800@TK2MSFTNGP15.phx.gbl...
> Try setting enforceconstraints to false before you try to do the updates,
> and then back to true once you are done.
>
> "Patrick" <patrick@nospam.com> wrote in message
> news:OIokHvunEHA.3460@tk2msftngp13.phx.gbl...
>> I have a situation in where I have a table with a primary key that can be
>> altered (in very rare occasions, but still have to deal with it) and the
>> change is cascaded in SQL Server to many other child tables.
>>
>> The problem is that this does not work in conjunction with datasets.
>>
>> When I first update the master table, the update of the child tables will
>> fail as the key of the child tables has been altered by the cascade and
> the
>> update command still uses the original key from the original row data in
> the
>> dataset.
>> When I first update the child tables, these will fail as the master table
>> does not yet contain the new key and thus this results in a foreign key
>> violation.
>>
>> Has anyone dealt successfully with this problem?
>>
>> Thanks,
>>
>> Patrick
>>
>>
>
>
- Next message: Kevin Yu [MSFT]: "RE: Why textboxes don't go blank when I click addnew"
- Previous message: Kevin Yu [MSFT]: "RE: Why textboxes don't go blank when I click addnew"
- In reply to: Marina: "Re: How to deal with cascading changes?"
- Next in thread: William \(Bill\) Vaughn: "Re: How to deal with cascading changes?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|