C# 使用EF(DB first)将DateTime参数传递给存储过程

C# 使用EF(DB first)将DateTime参数传递给存储过程,c#,sql-server,entity-framework,datetime,stored-procedures,C#,Sql Server,Entity Framework,Datetime,Stored Procedures,我在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_Perf

我在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.选择所有类型(新日期时间(2016-07-01)、新日期时间(2016-07-28)).ToList()

每次我通过传递参数调用它时,调试器都会显示一个运行时错误

但是如果我不带参数调用它,将存储过程更改为不带参数,并给它指定日期:

var sp = db.select_alltypes().ToList();
它返回预期的参数

错误是:

其他信息:执行命令时出错 定义。有关详细信息,请参见内部异常

将数据类型datetime2转换为datetime时出错


这是OP的反馈

无需对SQL表进行任何
datatype
更改,只需执行以下两个步骤

第1步:

将SP上参数的数据类型更改为如下所示:

@cdin_startunstufdate1 DateTime2=null,@cdin_startunstufdate2 DateTime2=null

第二步:

Sp调用应该是这样的

var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Par‌​se("2016-
07-28")).To‌​List();
var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Par‌​se("2016-
07-28")).To‌​List();
原始答案:

实体框架将所有
日期作为
Datetime2
处理

问题:

如果数据库中的字段是
Datetime
,则可能会出现问题

解决方案:

您必须将
cdindex
表的
cdin_startunstufdate
列日期字段数据类型更改为
datetime2

如何将
datetime
更改为
dDatetime2

只需转到SQL表的datetime列并更改它,如下所示

更新:

Sp调用应该是这样的

var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Par‌​se("2016-
07-28")).To‌​List();
var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Par‌​se("2016-
07-28")).To‌​List();

EF不会将所有datetimes视为DATETIME2,而是将所有越界或未初始化的日期视为DATETIME2。据我所知,使用
Nullable
来表示未初始化的日期时间而不是
DateTime
应该可以解决您的问题。

您能看看内部异常吗?@gilmishal通过扩展内部异常,我得到的消息是:将数据类型datetime2转换为DateTime时出错。实际上,如果你把这个内部异常复制/粘贴到谷歌,你就会得到答案。@uteist的可能副本当然是我做的,但没有解决方案。还有其他解决方案吗,在sql
Msg 5074,16级,状态1,第1行执行时,它会给我错误。索引“\u dta\u index\u Activity\u 7\u 202158240\u K11\u 9\u 10”依赖于列“acty\u Login”。Msg 4922,级别16,状态9,第1行ALTER TABLE ALTER COLUMN acty_登录失败,因为一个或多个对象访问此列我不知道DateTime2的转换是什么,但我正在工作,所以如果有人想使用SQL server处理数据库,在任何表中对Datetime列进行CRUD是否有任何问题?抱歉,我不明白你的意思?欢迎,谢谢你。你能提供一个例子吗?@Faresaya你可能正在使用代码优先的方法,并且在你的
上下文中
有一个类型为
DateTime
的字段,将其更改为
DateTime?
,它应该可以解决你的问题。@uteist不,我是先使用DB。不过,请检查您的上下文中的
DateTime
属性,并将它们转换为
nullable
*编辑:您也可以检查我在第一篇文章中的评论。@uteist您能给我解释一下如何将它们转换为nullable吗?