Roy Osherove

View Original

Bill Vaughn blogs, and I ask an ADO.Net question

  Bill Vaughn started a blog over at DotNetJunkies. Coolness!
I've been reading an hearing some of Bill's thoughts on Data Access in the recent past and all I can say is that I wish I knew all the stuff he's already forgotten about ADO.Net and Data Access techniques in general. Subscribed.
 
Now, here's something for Bill to gnaw on, since he specifically said 
“I've been known to answer questions when I can and I try not to make up the answer when I can't...” 
 
Context:
  • You have a Dataset that contains one parent table and one child table (with a relation between them).
  • The Parent table has a primary key comprising of Identity Values that needs to be reconciled with the database.
  • The Relation between the two tables is declared as “Cascade Update” and “Cascade Delete”.
  • You have a new row in the parent table and some new child rows for that row in the child table that you need to add to the database.
  • To save bandwidth, you use the GetChanges() method of the dataset and update those values.
  • The update succeeds and also retrieves back the new identity value of the new row (following all the steps mentioned in this MSDN article)
  • Identity columns are set to -1 with an increment of -1 to prevent database collisions
Problem:
Trying to merge the changed dataset with the original produces a ContraintException due to some sort of problem with the child rows in the child table(even though Cascade update is set to true). This problem does not occur if there are no new child rows to be updated as well.
 
Possible Solutions?
  • This article seems like a possible way to resolve this. What do you think?
  • I've heard on one of the recent .Net Rocks shows that Bill suggested approaching this problem by actually creating some empty rows on the database first instead of creating them in memory, thus preventing an constraint conflicts. However, it was mentioned as an afterthought to the conversation and I'm not sure that's the most elegant solution here.
  • [Added later] Here's Bill's article on this issue. I'll read it and see how it fits the problem.
  • [Added later]  Bill has many other cool and useful articles on his website. I'll have a look see when I get some time this weekend.