Rules in a ForeignKeyConstraint

I’ve noticed too many occasions when a fellow programmer comes up to me with this question:

“I’ve deleted a row and suddenly, another row got deleted! I checked my code and I don’t delete it anywhere… Must be some bug somewhere…”

So the answer in 99.9% of the cases is that you didn’t pay attention to the Rules in the foreign key constraints when you created your typed DataSet. Why? Because the defaults for UpdateRule and DeleteRule are Cascade (AcceptRejectRule defaults to None). This means that if you didn’t pay attention and you have a row that’s related to your row by foreign key, when you delete your row, that row will be marked as deleted.

My advice? Never have Rules set to Default. Microsoft could decide that what they’re doing today is wrong, change the defaults and you’re screwed.

Advertisements

3 thoughts on “Rules in a ForeignKeyConstraint

  1. It all comes back to the basic rule: If you don’t understand what code is generated, don’t use code generation tools.

    Datasets, strongly typed or otherwise, are serious code generated things.

    Personally, I never use datasets. Everything is in my domain model ( when not doing SOA ). Even then, my entities represent my data for me. Any functionality I need, I develop myself. I suggest you do the same. After doing it once or twice, it’s really not that big a deal. Besides, finally you’ll understand why all that code needs to be generated =)

  2. Udi- Self contradiction, eh? :)
    I prefer to use the Typed DataSet mechanism and plug into it if I feel something is not satisfying with the implementation (the DataSetGenerator class lets you do that, sorta).

    Andrew- Hehe, and to think that I develop with Oracle and used to develop with Access (I try to block those days from memory). ;P

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s