如何在ASP.Net MVC 5应用程序中重置Visual Studio提供的SQL Server中的主键计数器[重复]

时间:2021-02-21 15:37:59

This question already has an answer here:

这个问题在这里已有答案:

For learning purpose, I am developing one web site. And I have hardly run the website 100 times, but I don't know one of the table's primary key has reached 2012 count. And no way I have inserted even 100 records in total. I am not sure how did that happen. Currently, I have just 1 records in the table but their PK is 2013. I want it to reset to 0. I DO NOT Care about records being deleted as the web site is under development.

出于学习目的,我正在开发一个网站。我几乎没有运行过这个网站100次,但我不知道其中一个主要密钥达到了2012年的数量。我总是无法插入100条记录。我不确定那是怎么发生的。目前,我在表中只有1条记录,但它们的PK是2013年。我希望它重置为0.我不关心在网站正在开发中删除记录。

Below is the behavior:

以下是行为:

This is the VS Debugger output:

这是VS Debugger输出:

如何在ASP.Net MVC 5应用程序中重置Visual Studio提供的SQL Server中的主键计数器[重复]

如何在ASP.Net MVC 5应用程序中重置Visual Studio提供的SQL Server中的主键计数器[重复]

You can clearly see I have just one record but PK is 2013

你可以清楚地看到我只有一张唱片,但PK是2013年

如何在ASP.Net MVC 5应用程序中重置Visual Studio提供的SQL Server中的主键计数器[重复]

Controller Action

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CoverLetter coverLetter, HttpPostedFileBase uploadedCoverLetter)
        {
            var x = User.Identity.GetUserId();
            try
            {
                if (ModelState.IsValid)
                {
                    if (uploadedCoverLetter != null && uploadedCoverLetter.ContentLength > 0)
                    {
                        var tempcoverletter = new CoverLetter
                        {
                            FileName = System.IO.Path.GetFileName(uploadedCoverLetter.FileName),
                            ContentType = uploadedCoverLetter.ContentType,
                            CoverLetterName = coverLetter.CoverLetterName,
                            CandidateId = User.Identity.GetUserId(),
                            datetime = System.DateTime.Now
                        };
                        using (var reader = new System.IO.BinaryReader(uploadedCoverLetter.InputStream))
                        {
                            tempcoverletter.Content = reader.ReadBytes(uploadedCoverLetter.ContentLength);
                        }
                        _context.CoverLetters.Add(tempcoverletter);
                    }
                    _context.SaveChanges();
                    return RedirectToAction("CoverLetterCenter");
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return View("Index");
        }

1 个解决方案

#1


2  

IDENTITY columns is keep track and auto increment by SQL Server, I think it has nothing to do with Entity Framework.

IDENTITY列由SQL Server保持跟踪和自动递增,我认为它与Entity Framework无关。

Run this query on SQL Server

在SQL Server上运行此查询

DBCC CHECKIDENT ('CoverLetters', RESEED, 1)

DBCC CHECKIDENT('CoverLetters',RESEED,1)

#1


2  

IDENTITY columns is keep track and auto increment by SQL Server, I think it has nothing to do with Entity Framework.

IDENTITY列由SQL Server保持跟踪和自动递增,我认为它与Entity Framework无关。

Run this query on SQL Server

在SQL Server上运行此查询

DBCC CHECKIDENT ('CoverLetters', RESEED, 1)

DBCC CHECKIDENT('CoverLetters',RESEED,1)