repopulate primary key after linq to sql delete

Posted on 2009-04-24
Last Modified: 2013-11-11
Hi There experts,
I'm new to linq. I've got a delete function working in my windows form app using c# linq to sql to remove a row from my database. It works fine, I was just wondering if there was an easy way to reorder the primary key of the table the record was deleted from after the delete. by which i mean, currently if i have 3 records and i delete record number 2 then i will be left with a record with an id of 1 and a record with an id of 3. Is there a way to make the id's re-order themselves so in the above scenario when record 2 was deleted record 3 would now have an id of 2?
Question by:Jazzua
    LVL 21

    Accepted Solution

    This has nothing to do with Linq really, and resides more in SQL.

    Since your 'id' is a primary key, it is made automatically, and will *never* increment backwards.  Also, it can not be changes unless you take away the PRIMARY KEY and IDENTITY properties that you assigned to it, change the number, then re-assign those properties to the column - BAD IDEA!

    I don't really see the application of this as you could always  programmatically generate the number in the sequence if you needed to.

    But, what I would recommend if you *really* think you need this is to make another field as a psuedo-id that is a simple INT and use that one to keep your order.

    Author Closing Comment

    cheers :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now