9

自我回答问题以供将来参考:

如果您有一个删除并重新添加内容的表,则在插入 ID 等于现有 ID 的新行时会出现此错误:

Server Error in '/' Application.

A duplicate value cannot be inserted into a unique index. [ Table name = Stories,Constraint name = PK__Stories__000000000000001C ]

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlServerCe.SqlCeException: A duplicate value cannot be inserted into a unique index. [ Table name = Stories,Constraint name = PK__Stories__000000000000001C ]

Source Error:     

Line 57: 
Line 58:         Public Sub Save() Implements IStoryRepository.Save
Line 59:             context.SaveChanges()
Line 60:         End Sub

SQL Server CE 正在尝试插入 id (在本例中为 storyid)等于存在的行的行。

解决方案在这里:

http://msdn.microsoft.com/en-us/library/aa237859(v=sql.80).aspx

它看起来像这样: ALTER TABLE Stories ALTER COLUMN storyid IDENTITY (200, 1)

这意味着您修改表(Stories)的标识列(在本例中为storyid)并将其设置为比当前行所在的位置更大的数字。例如,如果我的 storyid 最大的行是 170,我可以这样播种:

ALTER TABLE Stories ALTER COLUMN storyid IDENTITY (171, 1)

下一个插入的故事 ID 为 171,然后它会自动递增。

卡尔

4

0 回答 0