When identity_insert is set to off, explicit values cannot be inserted for the identity_insert column in the table.

Source: Internet
Author: User

{"When identity_insert is set to off, explicit values cannot be inserted into the ID column in The 'orderlist' table "}

This exception can be handled from two perspectives: A: Database statement executionB: directly modify the ID node of the persistent class ing configuration file in nhib.pdf

Database a execution statement:

Problem description: when the primary key of the database table is designed as (orderidInt identity primary key), relative to this primary key identity_insert is set to off by default, that is, the value of the inserted primary key ID cannot be displayed, for example:

InsertIntoOrderlist (ID, ordername)Values ( 4520 , ' Electronic subpoena orders ' )

An error is prompted when you execute the preceding statement:

Server:Message 544 , Level 16 , Status 1 , Line 1
WhenIdentity_insertSetOffCannot be directed to the table ' Orderlist '  Insert an explicit value into the ID column.

When we want to set the record ID to custom 4520, the above error occurs. If we add some settings, the modification method is as follows:

-- Allow explicit values to be inserted into the table's ID columnOn - Allow Off - Not Allowed
Set  Identity_insertOrderlistOn -- Open

InsertIntoOrderlist (ID, ordername, createdate)
Values ( 4520 , ' Set ' , Getdate ())

Set  Identity_insertOrderlistOff -- Close

When executing this insert statement, an additional setting is added. The syntax of this setting is

-- Set Syntax:

SetIdentity_insert[Database .[Owner.]]{Table}{On| Off}
Allow explicit values to be inserted into the table's ID column

Parameter description:
Database: for Databases
Table: for a table

On: allows explicit value insertion.ID column
Off: Not Allowed

Note:

Code
-- Notes

(1) At any time, there is only one table in the session.Identity_insertAttribute can be setOn. If a table has set this attributeAnd sendsSetIdentity_insertOnStatement, thenMicrosoftSQLServerReturns an error message indicatingSetIdentity_insertSetOnIt is reported that this property has been setOnTable

(2) If the inserted value is greater than the current table id valueSQLServerThe new inserted value is automatically used as the current ID value.

(3) SetIdentity_insertIs set during execution or running, rather than during analysis.

In the preceding execution statement: add this setting before and after the statement to be executed. Of course, the insert operation for a record is described above. After this record is inserted, if the data is inserted again, if this setting is not enabled, the orderid primary key column will automatically increase to 4520 according to the custom value above. this operation is flexible and depends on how you use it.

B: modify the configuration file.

We can directly modify the configuration file to solve this problem,

File configuration when an error occurs:

-- Hibernate Persistence ConfigurationNote that the class attribute of the generator subnode in ID is: assigned automatically increases
< Hibernate - MappingXmlns = " URL: nhibernate-mapping-2.2 " >
     < Class  Name = " Testhibernateexpre. Entities. orderlistmodel, testhibernateexpre "  Table = " Orderlist " >
         < IDName = " Orderid "  Column = " ID "  Type = " Int " >
             <! -- Parameter settings in ID: Native / Assigned / Foreign / Increment -->
             < Generator Class = " Assigned " > </ Generator >
         </ ID >
     </ Class >

After modification, the file configuration is as follows:

-- Persistence class configuration in hibernate Note: generator subnodes under IDSet the class attribute to native.
< Hibernate - MappingXmlns = " URL: nhibernate-mapping-2.2 " >
     < Class  Name = " Testhibernateexpre. Entities. orderlistmodel, testhibernateexpre "  Table = " Orderlist " >
         < IDName = " Orderid "  Column = " ID "  Type = " Int " >
             <! -- Parameter settings in ID: Native / Assigned / Foreign / Increment -->
             < Generator Class = " Native " > </ Generator >
         </ ID >
       </ Class > From: http://blog.sina.com.cn/s/blog_7eecafbc0100rax7.html From: http://www.cnblogs.com/chenkai/archive/2009/04/13/1434606.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.