markjprice / cs12dotnet8

Repository for the Packt Publishing book titled "C# 12 and .NET 8 - Modern Cross-Platform Development Fundamentals" by Mark J. Price
636 stars 199 forks source link

Page 570 - Updating entities #69

Closed rmantel23 closed 2 months ago

rmantel23 commented 2 months ago

I do not know what the product ID will be for the products that you add. I do know that there are no products that start with “Bob” in the existing Northwind database. Finding a product to update using its name avoids having to tell you to first discover what the product ID is for a product that you’ve added. It is likely to be 78 because there are already 77 products in the table, but once you’ve added that and then deleted it, the next product to be added would be 79 and it all gets out of sync.

Is that correct? I have this:

| 076 | Lakkalikööri | $18.00 | 57 | False | | 077 | Original Frankfurter grüne Soße | $13.00 | 32 | False | | 078 | Bob's Burgers | $500.00 | 72 | False |

Then delete all Bob and have this:

| 076 | Lakkalikööri | $18.00 | 57 | False | | 077 | Original Frankfurter grüne Soße | $13.00 | 32 | False |

Then add Bob again:

| 076 | Lakkalikööri | $18.00 | 57 | False | | 077 | Original Frankfurter grüne Soße | $13.00 | 32 | False | | 078 | Bob's Burgers | $500.00 | 72 | False |

Bob was added again with 78.

markjprice commented 2 months ago

Are you using SQL Server or SQLite? I believe that SQL Server is "smarter" than SQLite and can "reuse" IDs if one is assigned but then the row is deleted before another is added. It all depends on the data provider. The point is, one shouldn't make assuptions about database-assigned values.

rmantel23 commented 2 months ago

This example was with SQLite. I was surprised that it reused the IDs actually. I get the point, and it's valid, but maybe just state more generally that you can't rely on IDs just as you said in your comment.

markjprice commented 2 months ago

I've implemented your suggestion for the 9th edition. (This section will be online-only, not in the print book.)