我进行单元测试将数据记录添加/插入到具有列名 Id 的表中,这是一个标识列。
在单元测试中,我手动插入了 id。所以我可以在 Insert 语句之前将 Identiy_Insert 设置为 ON。
为生产服务器启用此功能是否有任何缺点?
或者我应该改变我的单元测试方法吗?为 Name 字段插入一个值并检索
并且断言应该足以证明数据记录已插入以进行集成
清酒。
我进行单元测试将数据记录添加/插入到具有列名 Id 的表中,这是一个标识列。
在单元测试中,我手动插入了 id。所以我可以在 Insert 语句之前将 Identiy_Insert 设置为 ON。
为生产服务器启用此功能是否有任何缺点?
或者我应该改变我的单元测试方法吗?为 Name 字段插入一个值并检索
并且断言应该足以证明数据记录已插入以进行集成
清酒。
its not a feature you can keep ON. It is valid per connection.
give it a try, open a connection on SSMS and set it to ON to one table. Then open another connection and you will see that if you try to insert the IDENTITY on the same table, it will fail.
From MSDN:
At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, SQL Server returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.
In general, I would not recommend using IDENTITY_INSERT in production to avoid concurrency issues. As for the unit tests, I'd suggest creating a mocked data access layer instead of relying on a real database.