自我回答问题供将来参考:
如果您有一个删除并重新添加内容的表,则在插入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)并将其设置为比行当前所在位置更大的数字。例如,如果我的最大故事的行是170,我可以这样播种:
ALTER TABLE Stories ALTER COLUMN storyid IDENTITY (171, 1)
下一个插页的故事情节为171,然后会自动递增。
卡尔