This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, I have an entity with a primary key (KeyTable pattern) and an additional column that represents some other unique identifier. When creating the entity, we have a rule that states if a user does not provide this unique identifier, then we will populate it with the entity's primary key. The update is no problem since the entity will have a primary key assigned. When creating a new entitiy however, there is no primary key value. Do I need to save the entity in order to generate a primary key, then update the unique identifier and save again? Is there a better way to accomplish this or similar? Thanks. |
|
|
Since you are using KeyTable, the entity Id will be assigned as soon as you add it to a unit of work. So you can assign the unique identifier at any time after that. A good option is to do it in an OnSaving override: this avoids the need to catch all the various ways in which an entity can be added to a UOW. Note that you would NOT be able to do this if you were using the IdentityColumn identity method, because then the initial Id is a temporary one, and LightSpeed doesn't get the permanent Id until after saving. But all other identity methods, including KeyTable, allocate IDs up front rather than on save, and with them it will work just fine. |
|
|
Excellent. This worked out well as you suggested. One thing however, I did override the OnSaving method but I found that overriding OnValidating helped to shore up the entity state to pass validation. It seems I was able to get the Id as needed during that method call. Hopefully that won't get me in trouble. |
|
|
I'm having some problems setting the other unique identifier as described above. I'm using this method on an entity partial class: protected override void OnValidating(CancelEventArgs e) The problem here is OnValidating is being called multiple times. During the first call, the Id value is "0" and SurrogateId is null so the value is assigned. On a subsequent call to OnValidating when Id has the correct value from the key table, SurrogateId isn't being set since it's not null. I believe OnValidating is being called multiple times, once when I call Entity.IsValid and subsequently before the save? I'm just guessing here. I can't use the logic above in OnSaving though because a designer generated validate presence check on SurrogateId generates a validation exception. Any ideas? Maybe completely disable any LightSpeed automatically generated validators and move the logic to OnSaving? Help! |
|
|
That's right, the entity is validated when you call .IsValid and also before the unit of work is saved. I think you can fix this by changing your logic to: if (String.IsNullOrEmpty(SurrogateId) || SurrogateId == "0") { or: if (this.UnitOfWork != null && String.IsNullOrEmpty(SurrogateId)) { The latter uses the fact that the Id is assigned as soon as the entity joins a unit of work -- so if the if-test passes, the Id must have been assigned and can be safely used to generate a SurrogateId. Another possibility is to initialise SurrogateId to 'distinguished garbage' -- a value that is guaranteed to be unique (so that validation will pass) but that you can distinguish from real user input (so that you can replace it OnSaving if the user hasn't overridden it). For example, a sequence of nonprinting characters such as tab (\t) or alert (\a) followed by a randomly generated GUID. This is ugly but avoids the nastiness of OnValidating having side-effects on the entity state. |
|