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.