实体框架从存储过程创建错误的模型

时间:2017-08-21 12:39:31

标签: c# sql-server entity-framework tsql stored-procedures

我有一个存储过程“GET_PARTNER”,如下所示:

SELECT
...
Partners.name AS 'name'  -nvarchar
Partners.city AS 'city'  -nvarchar
NULL AS 'sales'          -money
NULL AS 'comments'       -nvarchar
...

我已将实体框架6添加到项目中,并将其设置为从数据库创建模型,并创建一个模型“GET_PARTNER_Result”,如下所示:

    //------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WSExport.Model
{
using System;

public partial class GET_PARTNER_Result
{
   public string name {get; set;}
   public string city {get; set;}
   public Nullable<int> sales {get; set;}    -wrong datatype
   public Nullable<int> comments {get; set;} -wrong datatype
}
}

如何修改存储过程,或者是否可以配置ef以便创建具有正确数据类型的模型? 像这样:

public Nullable<decimal> sales {get; set;}
public string comments {get; set;}

1 个答案:

答案 0 :(得分:3)

默认情况下,NULLINT数据类型。使用CAST

SELECT
...
Partners.name AS name                       --nvarchar
,Partners.city AS city                      --nvarchar
,CAST(NULL AS MONEY) AS sales               --money
,CAST(NULL AS NVARCHAR(1000)) AS comments   --nvarchar

快速检查元数据:

SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set(N'SELECT NULL AS c', NULL, 0)

<强> Rextester DEMO

您还可以使用以下方法检查整个商店程序的元数据:

SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set(N'EXEC GET_PARTNER params..', NULL, 0)