Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c# - Problems with IDENTITY_INSERT Set to OFF? :-/

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 IDENTITY_INSERT is set to OFF.

All I am trying to do is simply insert the users UserId into the UserId column, by using WebSecurity.CurrentUserId - Why is this not working?

I have:

dbase.Execute("INSERT INTO Orders 
                 (UserId, OrderId, Status) 
               VALUES 
                 (@0, @1, @2)", 
               WebSecurity.CurrentUserId, 
               Session["OSFOID"], 
               "Confirmed");`

So, as you can see, it's pretty simple. But, why won't it work?

My table definition is:

Table Definition

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...