I have a Transaction table that contains a TranRefId (for grouping purchase & reversal transactions together). The interface with the SQL database is using Entity Framework 4. I would like to be able to do 2 things…
- Explicitly specify a value for the TranRefId
- Auto-generate a value (whether by querying the MAX and incrementing or by some other means) when none is specified.
My concern, as this is a web application, is that there could will be multiple threads performing this action simultaneously. If I do Select Max(TranRefId) and increment the value, there is a strong chance that multiple transactions will end up with the same value (due to concurrency). Is there any solution within the Entity Framework to help with concurrency issues like this??
Also, TranRefId is only specific to the account. See sample data below…
Transaction Table (My apologies, don’t know how else to make a table on here…)
TranId__TranRefId__Account
1_________1________A
2_________1________A
3_________1________B
4_________2________B
5_________1________C
If you don’t have to have TranRefId column as an int you could make it a uniqueidentifier. Then you could generate a new Guid and set the value to that – knowing that it would always be unique.