Reputation: 1071
i have a mysql database table . The tablename is Question and i have a column name questionNo under that table . i have a web form that consists of a create button and some textbox , so every time i click the create button , it will create question number 1 , 2 so on into the database... but if i refresh the page and create again , it will still start from 1 . how do i check from database from questionNo column under tablename Question , that there is already question 1 , 2 or 3 so on .. so the next question i create after clicking the create button will be +1 of the questionNo column . Sorry for my bad English . I am new to entity framework , how to do this using entity ?
For example 3 rows of record with the questionNo 1 .. then when i create again , i might have 2 rows of record with questionNo 2 and so on . The problem now is that , after i refresh the page , i will be inserting multiple rows of record with questionNo 1 again
Upvotes: 0
Views: 304
Reputation: 63105
You can create auto increment primary key for Question table as below
CREATE TABLE Question
(
QuestionNo int NOT NULL AUTO_INCREMENT,
// other fields
PRIMARY KEY (QuestionNo)
)
MySql will do the increment when you insert new record, you don't need to handle it by the code
Using Entity Framework
If you want to get the current max value of QuestionNo
int maxQNo= context.Question.Max(q => q.QuestionNo);
check for newQNo
already added like below
bool alreadyAdded= context.Question.Any(q => q.QuestionNo == newQNo);
Upvotes: 1