Problems with IDENTITY_INSERT Set to OFF? :-/

后端 未结 6 839
情深已故
情深已故 2020-12-14 13:49

I\'m trying to insert order details into my DB, and it keeps saying:

Cannot insert explicit value for identity column in table \'Orders\' when IDENTIT

6条回答
  •  情书的邮戳
    2020-12-14 14:17

    Unless you enable the ability to do identity-insert (by setting identity-insert on for that table), you are NOT ALLOWED to touch that column - the database owns it.

    Either enable identity insert briefly, or: don't try to insert the UserId (let the DB create a new id).

    As per books online, SET IDENTITY_INSERT:

    SET IDENTITY_INSERT Orders ON
    INSERT INTO Orders (UserId, OrderId, Status) VALUES (@0, @1, @2)
    SET IDENTITY_INSERT Orders OFF
    

    More likely, though: if this is the Orders table, should the identity not be on OrderId ? You'd still have the same problem since you are trying to control the OrderId, of course.

提交回复
热议问题