When IDENTITY_INSERT is set to OFF, you cannot insert explicit values into the identity column in the table

Foreword:

During the reconstruction of the computer room today, when the operator returned the card to the customer, an error occurred and the data could not be inserted into the checkout table.

When IDENTITY_INSERT is set to OFF, you cannot insert an explicit value into the identity column in the table'CheckOut'

the reason:

1. Appears when the Insert statement is executed

2. At any time, the IDENTITY_INSERT property of only one table in the session can be set to ON. If a table has this property set to ON and a SET IDENTITY_INSERT ON statement is issued for another table, Microsoft® SQL Server™ returns an error message stating that SET IDENTITY_INSERT has been set to ON and reports that this property has been set to ON Table.

Solution:

Reference blog: https://www.cnblogs.com/wzzyuxi/p/11174958.html

1. I use the first method: allow explicit values ​​to be inserted into the identity column of the table

Add a sentence before our original database operation statement:

set identity_insert CheckOut(表名) ON

Make it

string sql = "set identity_insert CheckOut ON Insert into CheckOut Date=values @date";

This sentence means: the IDENTITY_INSERT property of the table can be set to ON

If you need to turn off (OFF), the code is:

set identity_insert OrderList OFF +  插入语句
ON:允许插入显式值插入 标识列
OFF:不允许

2. Method two:

Modify the configuration file

File configuration when error occurs:

--hibernate持久化类配置 注意id中Generator子节点设置Class属性为:assigned自动增长
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="TestHibernateExpre.Entities.OrderListModel,TestHibernateExpre" table="OrderList">
        <id name="Orderid" column="id" type="int">
            <!--id中参数的设置问:native/assigned/foreign/increment-->
            <generator class="assigned"></generator>
        </id>
    </class>

 Modified configuration file

--Hibernate中关于持久化类的配置  注意id下Generator子节点 class属性配置为native
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="TestHibernateExpre.Entities.OrderListModel,TestHibernateExpre" table="OrderList">
        <id name="Orderid" column="id" type="int">
            <!--id中参数的设置问:native/assigned/foreign/increment-->
            <generator class="native"></generator>
        </id>
      </class>

 

Guess you like

Origin blog.csdn.net/weixin_44690047/article/details/112380578