使用Identity(1,1)违反PRIMARY KEY约束

时间:2013-11-19 17:34:09

标签: c# sql-server entity-framework

我一直在努力解决一个不一致的问题,我希望能解决这个问题。在随机时间,我的系统将有一组4-5个INSERTS由于以下原因而失败:

  

违反PRIMARY KEY约束'PK_Quality_Checks'。无法在对象'dbo.Quality_Checks'中插入重复键。重复键值为(25943)。

我为此错误阅读的大多数其他问题都涉及使用Identity(1,1)以外的其他内容作为PK或非常专业的情况,因此我发布了自己的问题。

我正在使用Entity Framework来处理我的SQL Server 2012数据库。我将一个XML文件解析为一个名为Test_Run的对象,然后将该对象转换为我的Entity对象以添加到表中。

所有表格都使用Identity(1,1)作为主键。

我的转换代码:

public static int SaveTestRun(TestRun testRun)
{
        gReportEntities gReportEntity = new gReportEntities();
        var testRunTable = new Test_Runs()
        {
            TRX_Name = testRun.TrxFile,
            Start_Time = testRun.StartTime,
            Enviroment = testRun.Environment,
            Mach_Config = testRun.MachineConfiguration,
            Product = testRun.Product,
            Details = testRun.Details,
            Duration = testRun.Duration.ToString(),
            End_Time = testRun.EndTime,
            Failed_Num = testRun.FailedTestCount,
            Passed_Num = testRun.PassedTestCount,
            Incom_Num = testRun.IncompleteTestCount,
            Abort_Num = testRun.AbortedTestCount,
            NotEx_Num = testRun.NotExecutedTestCount,
            Timed_Num = testRun.TimedOutTestCount,
            QC_Failed_Num = testRun.FailedQualityChecksCount,
            QC_Passed_Num = testRun.PassedQualityChecksCount,
            QC_Incon_Num = testRun.InconclusiveQualityChecksCount,
            Video_Dir = testRun.VideoRootPath,
            Custom_Html = testRun.CustomHtml

        };

        foreach (var test in testRun.Tests)
        {
            var testResult = new Test_Results()
            {
                Test_Class = test.Value.TestClass,
                Test_Name = test.Value.Name,
                Result = test.Value.Outcome.ToString(),
                Start_Time = test.Value.StartDate,
                Guid = test.Value.Id,
                Duration = test.Value.Duration.ToString(),
                Category = String.Join(", ", test.Value.Categories),
                Error_Message = test.Value.ErrMessage,
                Stack_Trace = test.Value.StackTrace,
                Video_Link = test.Value.VideoPath,
                Comment = test.Value.Comment
            };

            foreach (var qCheck in test.Value.QualityChecks)
            {
                var qualityCheck = new Quality_Checks()
                {
                    Check_Name = qCheck.Name,
                    Check_Outcome = qCheck.Outcome.ToString(),
                    Message = qCheck.Message
                };
                testResult.Quality_Checks.Add(qualityCheck);
            }

            testRunTable.Test_Results.Add(testResult);
        }

        gReportEntity.Test_Runs.AddObject(testRunTable);

        gReportEntity.SaveChanges();

        return testRunTable.Test_Run_Id;
}

我需要修复此问题,否则我的工具无法完全实现。

1 个答案:

答案 0 :(得分:2)

通常,如果标识列违反了键约束,那是因为某些内容已添加到您的表中,其ID高于其当前播种的值。因此,当您插入新值时,它会发现已存在具有该ID的条目。

你可能想要reseed your identity column