IDENTITY_INSERT 设置为 OFF 时出现问题? :-/

发布于 2024-11-19 15:31:46 字数 625 浏览 4 评论 0原文

我正在尝试将订单详细信息插入我的数据库,但它一直显示:

当 IDENTITY_INSERT 设置为 OFF 时,无法在表“Orders”中插入标识列的显式值。

我想做的只是使用 WebSecurity.CurrentUserId 将用户 UserId 插入到 UserId 列中 - 为什么这不起作用?

我:

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

所以,正如你所看到的,这非常简单。但是,为什么行不通呢?

我的表定义是:

Table Definition

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拥有 2024-11-26 15:31:47

有趣的是,我发现当我使用“Select.....Into [new table] from [tables contains data]”创建表时,我随后无法插入新记录,将 Insert_Identity 设置为关闭消息。
我通过删除表然后使用创建表脚本重建它来解决这个问题,现在根据需要插入尽可能多的新 ID 没有问题

Interestingly I found that when I created a table using a "Select.....Into [new table] from [tables containing data]" I subsequently could not Insert new records, getting the Insert_Identity set to off message.
I got around this by deleting the table then using a Create Table script to rebuild it and now have no problems inserting as many new IDs as needed

长亭外,古道边 2024-11-26 15:31:46

除非您启用执行身份插入的功能(通过为该表设置身份插入on),否则您不允许触摸该表列 - 数据库拥有它。

要么短暂启用身份插入,或者:不要尝试插入 UserId(让数据库创建一个新的 ID)。

根据在线书籍,SET IDENTITY_INSERT

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

不过,更有可能的是:如果这是 Orders 表,那么标识不应该位于 OrderId 上吗?当然,由于您试图控制 OrderId,您仍然会遇到同样的问题。

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.

孤单情人 2024-11-26 15:31:46

通常,您不希望将整数插入主键列。通常,仅当您希望 SQL Server 在插入时在此列中设置自动递增整数时,才将该列的“Identity”标志设置为 true。

与 Marc Gravell 的答案一样,您可以使用启用身份插入,

SET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }

但是在常规应用程序代码中执行此操作确实不健康 - 您最终会遇到并发问题,并且很可能会出现重复的身份。更好的是,不要插入订单的 ID - 让数据库自动为您完成此操作,然后只需使用 @@IDENTITY 查询新 ID(或者更好的是,SCOPE_IDENTITY()< /代码>)。

如果出于某种原因您确实需要存储用户的会话 ID,请将其作为订单表上的单独列,或者更好的是,在单独的用户表上,并将 UserId 作为外键。

Generally you would not want to insert an integer into a primary key column. You would usually set the column's "Identity" flag to true only where you wanted to have SQL Server set an auto-incrementing integer into this column on insert.

As with Marc Gravell's answer, you can enable identity insert using

SET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }

But doing this in regular application code is really unhealthy -- you'll end up with concurrency issues and quite likely duplicate identities. Better, don't insert the Order's ID -- let the DB do it for you automatically, and then simply query for the new ID using @@IDENTITY (or better, SCOPE_IDENTITY()).

If for some reason you definitely need to store the user's session id, make this a separate column on the order table, or better still, on a separate User table, with the UserId being a foreign key.

顾冷 2024-11-26 15:31:46

您不希望 UserID 成为身份,我也不认为您希望 UserID 成为主键。您很可能希望 OrderID 作为主键,或者最多与 userid 和 orderid 共享主键。

You do not want the UserID to be an Identity, and I also do not think you want the UserID to be the primary key either. More than likely, you want the OrderID to be the primary key, or at best shared primary key with the userid and orderid.

虫児飞 2024-11-26 15:31:46

在表的定义中将 Is Identity? 设置为 False,但不设置不能手动向 UserID 插入值

In your table's definition set Is Identity? as False, with out setting that you cant insert a value manually to the UserID

樱娆 2024-11-26 15:31:46

您已将IS Identity设置为YES,这就是为什么现在您无法为此列插入值的原因数据库将通过递增值自动插入它。

我看到您将UserId设置为表的主键这是错误的 Order OoderID 应该是列的主键。

将 UserID IsIdentify 设置为 false
并将 OrderID IsEdentitfy 设置为 yes 并使其成为主键列。

You have set IS Identity to YES that's why now you cant insert value for this column DB will automatically insert it by incrementing values..

And the thing i am seeing you set UserId as the primary key of the table which is wrong Order OoderID should be the primary key of the column.

Set UserID IsIdentify to false
and OrderID IsEdentitfy to yes and made it primary key column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文