I am trying to auto increment data, which is not Id, I have used Sql Identity on Id so can't use it anymore. I tried sql sequence to bind data column and it works if I add information from sql via script but it does not work on adding from visual studio. I tried to [DatabaseGenerated(DatabaseGeneratedOption.Computed)] but it does not work too. I have been searching this topic for 2 days in web but can't find any solution
-
Look this: https://stackoverflow.com/questions/16079217/how-to-generate-and-auto-increment-id-with-entity-framework – Rogerio Azevedo Mar 13 '19 at 13:08
-
no, that is on Id and uses identity – lekinio Mar 13 '19 at 13:55
-
@lekinio, I am looking for this solution as well. Did you find anything? I have a workaround by maintaining a sequence in another table and using it for a non-id column. However, to avoid concurrency, I will need some locking mechanism. – prinkpan Aug 02 '19 at 17:45
1 Answers
You can use SQL Sequences for this. According to the documentation by Microsoft
A sequence is a user-defined schema bound object that generates a sequence of numeric values according to the specification with which the sequence was created. The sequence of numeric values is generated in an ascending or descending order at a defined interval and can be configured to restart (cycle) when exhausted. Sequences, unlike identity columns, are not associated with specific tables. Applications refer to a sequence object to retrieve its next value. The relationship between sequences and tables is controlled by the application. User applications can reference a sequence object and coordinate the values across multiple rows and tables.
You can create a sequence using the syntax below:
CREATE SEQUENCE OrderNumber
START WITH 1
INCREMENT BY 1 ;
GO
To check how it can be used in MVC, please check this post.
Hope this helps!

- 2,117
- 1
- 19
- 32