插入时类型化数据集 ConstraintException

发布于 2024-09-14 13:44:52 字数 758 浏览 2 评论 0原文

基本上我有一个包含分层数据的表。

所以我有一个 id 列和一个汇总列,该列指向每个孩子的父母的 id 列。

该表是使用 TableAdapter 上的 GetData() 从数据库加载的,然后使用以下命令设置表的主键自动增量种子:

SELECT IDENT_CURRENT('表名') + 1

在层次结构中创建新的子级,然后我在同一个 TableAdapter 上调用 Update()。

大多数情况下,这工作得很好,但偶尔我会遇到 ContraintException Column 'id' is constrained to be unique 问题,并且它似乎只在添加新行时才会发生。

通常,我只需将 autoincrementseed 和 autoincrementstep 设置为 -1,更新 id 列后将从数据库中获取正确的值,但由于汇总列取决于主键值,因此这是行不通的。

我也无法实时应用每个新行,因为所有更改作为一个整体都必须由用户审查,然后才能应用到数据库。

有什么想法吗?

编辑:我应该提到的是,没有任何其他更改从任何其他来源进入该表,因此这里不存在并发问题。

另一个编辑:今天,我在调用更新方法之前检查了表中的值,并注意到问题与新行未在主键上正确排序有关。

例如,行是 960,961,963,962,当插入 962 时,我得到了 962 已经存在的错误。如果更新按顺序插入行,则不会发生这种情况。

为什么表不按主键顺序索引行?

Basically I have a table that contains hierarchical data.

So I have an id column, and a rollup column which points to the id column for each child's parent.

This table is loaded from the database using GetData() on the TableAdapter then the table's primary key autoincrementseed is set using:

SELECT IDENT_CURRENT('TableName') + 1

New children are created in the hierarchy, then I call Update() on the same TableAdapter.

Most of the time this works perfectly, but occasionally I get the ContraintException Column 'id' is constrained to be unique problem, and it only seems to occur when new rows are added.

Normally, i would just set the autoincrementseed and autoincrementstep to -1 and after updating the id column would get the correct values from the database, but because the rollup column depends on the primary key values this won't work.

I can't apply each new row live either because all the changes as a whole must be reviewed by the user before they are applied to the database.

Any ideas?

EDIT: I should mention that no other changes are coming in to this table from any other source, so there are no concurrency issues here.

ANOTHER EDIT: Today I checked the values in the table before the update method was called and noticed that the problem is related to the new rows not being properly sorted on the primary key.

So for example the rows went, 960,961,963,962 and when 962 was inserted I got the error that 962 already exists. Had the update inserted the rows in order this would not have happened.

Why would the table index the rows not in order by primary key?

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

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

发布评论

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

评论(1

む无字情书 2024-09-21 13:44:52

使用 SELECT IDENT_CURRENT('TableName') + 1 会杀死你,除非你只有一个用户

using SELECT IDENT_CURRENT('TableName') + 1 will kill you unless you only have one user

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