System.Data.SqlClient.SqlException: Data type text and varchar are incompatible in the equal to operator.

Source: Internet
Author: User
Tags microsoft sql server

First, the source of the cause

Environment: vs2015,sqlserver2008

Related packages: Ef6

Defines an entity article

 Public  class article{    publicstring data{get;  Set;}}

Configuration of the Entitytypeconfiguration

Property (A = A.data). IsRequired (). Hascolumntype ("text");

This allows table fields to be generated normally, and the type is correct
When inserting data, it causes the following exception

System.Data.SqlClient.SqlException: Data type text and varchar are incompatible in the equal to operator.

Second, the settlement

At first, I thought it was not specified to cause a conversion error

[Column (Typename="text")]  Public string data{get;  Set;}

Insert data again, or cause the same error

After many tests,

The end result is that the type is not specified as text and the system automatically specifies the type nvarchar (MAX)

Third, the analysis

By SQL2005, if a varchar has a defined number of characters, then the maximum is 8000, which will result in a binary truncation.

varchar (max), nvarchar (max), and varbinary (max) are collectively referred to as large-value data types. You can store data up to 2^31-1 bytes.

|  18,2^-1  bytes. In Microsoft SQL Server The ntext, text, and image data types will be removed in future versions of the Avoid using these data types in new development work, and consider modifying applications that currently use these data types. 


When storing character length <=8000, the storage mechanism, like regular varchar, actually occupies space = character length +2 (end identifier).
When storing character lengths >8000, the storage mechanism is the same as text.

The maximum length supported by varchar (max) is the maximum length supported by the system, such as a maximum length of 2G bytes on 32-bit SQL Server.

When the character length is less than or equal to 8000, no matter how much the varchar or varchar (max) is saved.


System.Data.SqlClient.SqlException: Data type text and varchar are incompatible in the equal to operator.

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.