Got a problem whereby a trigger has been added to a table which inserts a row into another table. Both tables involved have identity fields as primary keys.
The issue is that any attempt to insert rows into the original table now produces a “record not found” error from the framework.
I guessed that, when the framework is attempting to retrieve the newly inserted identity field for the original table, it’s actually retrieving the identity field from the secondary table the trigger is inserting into, thus it gets the wrong value and can’t locate the newly inserted record.
Tracing the SQL issued to the database appears to confirm this - you’re using @@IDENTITY rather than SCOPE_IDENTITY.
I don’t really want to start writing stored procedures to handle all my inserts just to get around this issue.
I’m sorry but this is a fundamental design flaw. Scope_Identity was introduced specifically because @@IDENTITY has this limitation and doesn’t work with triggers which insert into a secondary table. That the DA framework is incapable of correctly retrieving the identity value on any table with such a trigger amazes me and is going to cause me huge problems. Has no one else raised this problem before?
I did think of that but it’s dangerous as any manipulation of these records outside of the App Server (for example running scripts directly against the database to fix problems etc) wouldn’t generate the records in the trigger table.
I’ve managed to work around it by replacing the identity field with a GUID using newsequentialid() to prevent fragmentation problems but I still think this is a huge oversight and I’m stunned it hasn’t come up before. I really think you should review this internally at some point.