存储过程空列在实体框架中显示其类型为Int

时间:2022-03-14 02:17:13

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:

我在存储过程中有这个SELECT查询,在实体框架中,它显示属性类型int。存储过程如下:

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:

我希望将null作为备注列值显示为EF中的字符串。现在是:

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

But it must be like this:

但它一定是这样的:

public string Remarks { get; set; }

1 个解决方案

#1


3  

Use CAST to get desired datatype(length):

使用CAST获得所需的数据类型(长度):

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:

空值默认为INT数据类型:

-- 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

LiveDemo

Output:

输出:

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

#1


3  

Use CAST to get desired datatype(length):

使用CAST获得所需的数据类型(长度):

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:

空值默认为INT数据类型:

-- 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

LiveDemo

Output:

输出:

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