I would create a custom id generator (without using identity nor stored proc) :
I've the following actions with EF :
- Read : get the row and read the counter field
- Checks: perform some checks, generate & increment counter
- Write : update the row for future use
Naturally we should not have a duplcate id !
I started by putting the operations (Read and Write) in a transaction _context.Database.BeginTransaction
- but not sure what is the appropriate isolation level ?
- I read that Repeatable Read ensure that read-data can't be updated by conccurent transaction, but what about the scenario when : Tran1 Read 3, Tran2 Read 3, Tran1 Update to 4, Tran2 Update to 4 !!! Could the Tran1 update be done if so how Tran2 could read new value after Tran1 has completed ?
- I found also that : SERIALIZABLE do the opposite, and locks everything. While READ COMMITTED releases the locks when it read the row, or when the statement is completed depending on the lock, SERIALIZABLE releases the locks once the transaction is committed. This means that another transaction that wants to update data that the transaction have read at least once, or another transaction wants to read data that the transaction has updated will be blocked until the transaction is committed.
- or should i use a locking mechanism to prevent read-data to be used by a concurrent tran for generating a wrong id ?