我在SQL Server 2012中创建了一个存储过程
ALTER proc [dbo].[select_alltypes]
@cdin_startunstufdate1 DateTime = null,
@cdin_startunstufdate2 DateTime = null
AS
BEGIN
SET NOCOUNT ON;
SELECT
0 as shipid,
i.cdin_cdindexid, p.pinv_PerformaInvID,
coalesce(i.cdin_serial, 0) as depno,
coalesce(convert(datetime, left(convert(nvarchar, i.cdin_startunstufdate, 120), 10), 120),'-') as deidate,
coalesce(i.cdin_goodsDesc, '-') as gooddesc,
coalesce(i.cdin_Customdeclar, '-') as custdec,
coalesce(i.cdin_NoofPackages, 0) as pkg,
coalesce(i.cdin_WT, 0) as wt,
coalesce(i.cdin_volumewt, 0) as vwt,
coalesce(i.cdin_MortgageAmount, 0) as lcamt,
coalesce(p.pinv_name, '-') as invno,
coalesce(p.pinv_TotalAmount, 0) as invamt,
p.pinv_Status, p.pinv_InvoiceProperty as prop,
coalesce(c.comp_name, '-') as custname,
coalesce(Comp_CompanyId, '-') as custid,
coalesce(c.comp_idcust, '-') as accpacno,
coalesce(t.Terr_Caption, '-') as Terr,
convert(nvarchar, '01', 2) as type
from
cdindex i
inner join
company c on i.cdin_CompanyId = c.Comp_CompanyId
inner join
Territories t on i.cdin_Secterr = t.Terr_TerritoryID
left outer join
PerformaInv p on i.cdin_cdindexid = p.pinv_CDIndexId
where
(cdin_deleted Is null And c.comp_deleted Is null
And t.Terr_Deleted Is null And p.pinv_deleted Is null)
and cdin_startunstufdate between @cdin_startunstufdate1 and @cdin_startunstufdate2
and (p.pinv_status in ('Draft', 'Posted') or pinv_status is null)
and (p.pinv_InvoiceProperty = '01' or p.pinv_InvoiceProperty is null )
end
我想用这样的EF来调用这个过程:
var sp = db.select_alltypes(new DateTime (2016-07-01), new DateTime (2016-07-28)).ToList();
每次调用传递参数时,调试器都会显示运行时错误。
但是如果我在没有参数的情况下调用它,将存储过程改为没有参数并给它指定日期:
var sp = db.select_alltypes().ToList();
返回预期参数。
错误是:
附加信息:执行命令时发生错误 定义。有关详细信息,请参阅内部异常。
将数据类型datetime2转换为datetime
时出错
答案 0 :(得分:2)
EF并未将所有日期时间视为DATETIME2,它将所有越界或未初始化日期视为datetime2。根据我的理解,使用Nullable<DateTime>
代表未初始化的日期时间而非DateTime
可以解决您的问题。
答案 1 :(得分:0)
这是 OP的反馈。
无需对SQL表进行任何datatype
更改,只需执行以下两个步骤。
第1步:
将SP上参数的@cdin_startunstufdate1 DateTime2=null, @cdin_startunstufdate2 DateTime2=null
更改为:
var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Parse("2016-
07-28")).ToList();
第2步:
Sp call应该是这样的。
dates
原始答案:
实体框架将所有Datetime2
处理为Datetime
,
问题:
如果数据库中的字段为cdindex
,那么这可能是个问题。
解决方案:
您必须将cdin_startunstufdate
表的datetime2
列日期字段数据类型更改为datetime
。
如何将dDatetime2
更改为var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Parse("2016-
07-28")).ToList();
只需转到SQL表的datetime列,然后按如下所示进行更改。
更新:
Sp call应该是这样的。
{{1}}