In a previous posting I described in some detail an all-too-complicated and restrictive mechanism for handling server generated key values which was, at the time, the best plan we had come up with. Since then, happily, we have been able to dramatically simplify things. The quick summary is:
You no longer need to specify server generated key values or client auto-generation in the CSDL file. Configure the server to generate values automatically and the framework automatically takes care of the rest.
Even better, the new mechanism works for all data types instead of just the restricted list of types supported by the old system.
Here's what we changed to get the new system to work:
Now the flow of a typical new entity with a server-generated key value looks something like this:
Presto! Like magic, configuring automatic key values for the entity framework is the exact same as configuring them on the server.
Unfortunately, there's no such thing as a free lunch. In this case there's one main downside to the new system: If your entity key values are not server generated and you add two entities with the same key values to the same ObjectStateManager, in the old system this would be detected the moment you tried to add the second entity and an exception would be thrown. In the new system, however, both added entities will be given temporary keys so the ObjectStateManager will not detect the conflict at that point. When you call SaveChanges on the context, one of the added entities will save successfully while the second one will fail due to unique value constraints on the server. Fortunately, the EntityAdapter performs the inserts within a transaction so this failure will cause the full set of changes to roll back and you will then have an opportunity to correct the problem, but the failure comes at a later time than would be ideal. After much debate within the product team our conclusion was that the simplicity and reliability for the most common cases (either server generated values or natural keys which are unique) outweighed the late-failure in the case where there are duplicate key values.
So, when can I get my hands on this new system, you ask? The February CTP.
This is indeed very great news... can't wait for the FebCTP!