Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set auto-increment via SQL Server Express Management Studio?

I just tried inserting value in to a database and that work. Now I insert again and I get an error for identical primary key.

I can't find any option to alter it to be auto-increment.

I'm updating the table via Linq-To-Sql.

User u = new User(email.Text, HttpContext.Current.Request.UserHostAddress,
                                        CalculateMD5Hash(password.Text));
db.Users.InsertOnSubmit(g);
db.SubmitChanges();

I didn't fill in the user_id and it worked fine the first time. It became zero. Trying to add a second user, it wants to make the ID 0 again.

I could query the database and ask for the highest ID, but that's going to far if you know about auto-increment.

How can I turn this on? All I can find are scripts for table creation. I'd like to keep my existing table and simply edit it.

like image 933
KdgDev Avatar asked Dec 07 '10 16:12

KdgDev


People also ask

How can create auto increment in SQL Server?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .

How do I create an existing column auto increment in SQL Server Management Studio?

Go to Identity Specifications and explore it. Make (Is Identity) row as Yes and by default Identity Increment row and Identity Seed row become 1. In case we want to automatically increase the value of this column by 2 (like 1, 3, 5, 7 etc.) then change the value of Identity Seed to 2.

How can I change column auto increment in SQL Server?

ALTER TABLE Inventory MODIFY COLUMN item_number INT AUTO_INCREMENT=50; After running this code, future item IDs will start at an item_number of 50 and increment by 1. To change the starting increment value and increment in SQL Server, set your non-default values during table creation.

How do you set up AutoIncrement?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name. The name of the table whose AUTO_INCREMENT value you wish to change.


2 Answers

How is your Linq-to-SQL model defined?? Check the properties of the user_id column - what are they set to??

alt text

In your Linq-to-SQL model, be sure to have Auto Generated Value set to true, Auto-Sync set to OnInsert, and the server data type should also match your settings (INT IDENTITY),

In SQL Server Management Studio, you need to define the user_id column to be of type INT IDENTITY - in the visual table designer, you need to set this property here:

alt text

like image 101
marc_s Avatar answered Sep 30 '22 21:09

marc_s


It is zero because you have a integer for a primary key column type. To use auto-increment, set tables identity column to the ID (selected in the table properties)

like image 44
Denis Biondic Avatar answered Sep 30 '22 20:09

Denis Biondic