Linq To Sql: Exception "String must be exactly one character long"

Consider a SQL Server table defined with a field varchar(1) NULL. It is used to store gender. Some lines contain data, and some do not: either empty or empty. The spaces provided MUST be zeros, but consider that this is an empty value. I would prefer the value to be zero.

ID Gender
1 'M'
4 'M'
3 ''
4 'F'

An exception occurs when executing a Linq To Sql query, where the value someIDis 3.

var emp = (from e in db.Employees
           where e.ID == someID
           select e);

Exception

The string must be exactly one character long.

Question : What is the reason for this exception? What can be done to prevent or eliminate this problem?

+3
3

Employee, LINQ to SQL. , Gender - System.Char ( , LINQ to SQL varchar(1)), System.String, .

, LINQ to SQL varchar(1) System.Char, , , T-SQL:

declare @foo varchar(1);
set @foo = '';

. #:

Char foo = '';

, System.String.

.. , , .

+15

, , '', ? . , ( ).

, , LINQ , , , .

, , / , , NULL ( NULL)

0

This issue also occurs when Linq To SQL Designer attempts to automatically generate a result class to store the results of the stored procedure. Copy the automatically generated class to your own new class file (use its name) and make changes to it. Each time you update your DataContext, this is simply a case of deleting an automatically generated class. Not an ideal solution, but a workaround for 2008.

0
source

Source: https://habr.com/ru/post/1713693/


All Articles