C
C
CooperUR2014-04-02 11:24:14
SQL
CooperUR, 2014-04-02 11:24:14

Why doesn't autoincrement work when creating a record in MS SQL database?

When adding a second category (the first one was added normally) , it swears that there is already a value with the same id
.
visual studio 2010, MVC3, entity framework, SQL Manager lite, code generated by CRUD Categories
table:

[Id] int IDENTITY(1, 1) NOT NULL,
[ParentId] int NOT NULL,
[Title] nchar(255) NOT NULL,
CONSTRAINT [categories] PRIMARY KEY NONCLUSTERED ([Id])
  WITH (
    PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF,
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON
)

How to make the id automatically inserted in ascending order?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CooperUR, 2014-04-02
@CooperUR

I figured it out, the point was that I needed to declare the data that I want to write to the database through the model.
If you don’t declare it, an empty id field is sent from the page for adding a category, which is equal to 0 (since the id in the database is not null) and is already present in Thank
you all, issue resolved

A
Alexander, 2014-04-02
@Papagatto

Maybe you have an Id field in the insert statement?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question