Windows Azure SQL Database - Identity Auto increment column skips values

前端 未结 4 1341
忘了有多久
忘了有多久 2020-12-05 08:09

Currently working on an ASP.Net MVC 4 application using Entity Framework 5. Used CodeFirst for initial development phase. But have now disabled the Automatic Migrations and

4条回答
  •  Happy的楠姐
    2020-12-05 08:34

    Try the reseeding with trigger approach. I believe this should solve it example of its use and see more walkarounds at that link.

    USE [TEST]
    
    CREATE TABLE TEST(ID INT IDENTITY(1,1),VAL VARCHAR(10))
    
    CREATE TRIGGER TGR_TEST_IDENTITY ON TEST
    FOR INSERT
    AS
    DECLARE @RESEEDVAL INT
    SELECT @RESEEDVAL = MAX(ID) FROM TEST
    DBCC CHECKIDENT('TEST', RESEED, @RESEEDVAL)
    
    INSERT INTO TEST(VAL)VALUES('a')
    
    SELECT * FROM TEST
    

    Since 'DBCC CHECKIDENT' is not supported in Azure now you can use the approach in this link In that link i got some work arounds

    1. Use GUID as key when using auto key of your SqlAzure
    2. If integer key like my case let the record insert and go back and delete it and re insert it with the right key by Turing off identity with set identity_insert XXXTable on -- this basically turns off IDENTITY

    and then turning back on identity again when i am through with the insertion with the right key using

    set identity_insert XXXTable off --this basically turns on IDENTITY

    Note: this is not a good solution for a table that is receiving a massive insert request but might be useful for someone looking for a temporary way out

提交回复
热议问题