Instead of trigger in SQL Server loses SCOPE_IDENTITY?

后端 未结 6 2061
走了就别回头了
走了就别回头了 2020-12-01 10:30

I have a table where I created an INSTEAD OF trigger to enforce some business rules.

The issue is that when I insert data into this table, SCOPE_I

6条回答
  •  臣服心动
    2020-12-01 11:15

    Main Problem : Trigger and Entity framework both work in diffrent scope. The problem is, that if you generate new PK value in trigger, it is different scope. Thus this command returns zero rows and EF will throw exception.

    The solution is to add the following SELECT statement at the end of your Trigger:

    SELECT * FROM deleted UNION ALL
    SELECT * FROM inserted;
    

    in place of * you can mention all the column name including

    SELECT IDENT_CURRENT(‘tablename’) AS 
    

提交回复
热议问题