user3515453
user3515453

Reputation:

Stored procedure null column shows its type as Int in an Entity Framework

I have this SELECT query in a stored procedure and in Entity Framework it shows property type int. How can I make the property type string? Here is the stored procedure:

alter procedure EquipmentRequest
as 
SELECT 
e.EquipmentName,  e.Id,
  null as Remarks
FROM
PECEquipment as e

I want the null as Remarks column value to be shown as string in EF. Right now its as:

public Nullable<int> Remarks { get; set; }

But it must be like this:

public string Remarks { get; set; }

Upvotes: 2

Views: 554

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175706

Use CAST to get desired datatype(length):

ALTER PROCEDURE EquipmentRequest
AS 
BEGIN
  SELECT e.EquipmentName,
         e.Id,
         CAST(NULL AS VARCHAR(100)) AS Remarks   
  FROM PECEquipment as e;
END

NULL by default has INT datatype:

-- checking metadata
SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set(
     N'SELECT NULL AS Remarks', NULL, 0)  
UNION ALL 
SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set(
     N'SELECT CAST(NULL AS VARCHAR(100)) AS RemarksCasted', NULL, 0)  

LiveDemo

Output:

╔═══════════════╦══════════════════╗
║     name      ║ system_type_name ║
╠═══════════════╬══════════════════╣
║ Remarks       ║ int              ║
║ RemarksCasted ║ varchar(100)     ║
╚═══════════════╩══════════════════╝

Upvotes: 3

Related Questions