Adding unique index in Entity Framework prevents DataTables display working

后端 未结 1 638
天命终不由人
天命终不由人 2021-01-22 17:16

It\'s a very simple structure - parent child with foreign key 1 to many.

Was working fine but I wanted to enforce uniqueness by adding a composite unique index as so:

1条回答
  •  情歌与酒
    2021-01-22 17:18

    EDIT 3 LOOKS LIKE COULD BE SOLVED WITH WORKAROUND

    I never knew this could be so stupidly unpredictable like this thinking it would just take care of itself with the auto increment and never imagined it would have such a prohibitive effect on Datatables rows display but any how it looks like this could work on initial testing unless it somehow breaks as I've only tested it once with the deletion and subsequent addition of records. Fingers crossed it is a suitable work around anyway unless someone can get to the bottom of it and offer a better solution.

    Looks like I am going to have to run this piece of code every time I perform a deletion - resetting the maximum identity value to be equal to the current identity value in the PK.ID column after every deletion as shown below:

    DECLARE @maxIdentityETValue INT
    DECLARE @maxIdentityFValue INT
    
    SET @maxIdentityETValue = (SELECT MAX(Id) FROM ExchangeTypes)
    DBCC CHECKIDENT('ExchangeTypes', RESEED, @maxIdentityETValue)
    
    SET @maxIdentityFValue = (SELECT MAX(Id) FROM Fixtures)
    DBCC CHECKIDENT('Fixtures', RESEED, @maxIdentityFValue)
    
    DBCC CHECKIDENT ('ExchangeTypes', NORESEED) 
    DBCC CHECKIDENT ('Fixtures', NORESEED)
    

    EDIT 2 NOT SOLVED.

    Tried deleting and then adding records and further to the deleting it would not allow any subsequent additions to be displayed anymore. :(

    See the step up in Identity generated ID values - that's what's causing the problem - well at least if I dont reseed those numbers after deletions and before subsequently adding records then Datatables displays the new records. But if I dont reseed them further to deleting records and before subsequently adding records it is still preventing datatables from displaying the records and shows the above error.

    Maddening - someone please help me!


    EDIT 1: Solved. Well kind of.

    When adding an index, well a unique index at least, it is necessary to reseed both parent and child PK Identity generated ID values in order that the datatables work - displaying the records error free.

    Don't ask me why but it just works.

    Further to adding my unique enforcing constraints in the indexes, parent and child respectively, I after reseeding both identity columns as descibed, I am now able to add new records to the database and datatables displays them fine.

    0 讨论(0)
提交回复
热议问题