EntityFramework 日期时间错误中的存储过程

StoredProcedure In EntityFramework DateTime Error

在我的项目中,我使用了 EntityFramework 5,我想在我的上下文中使用 SP。像这样

public virtual object GetEvaluations(int AgentId, DateTime StartDate, DateTime EndDate, int FormId)
{
    DataTable table = new DataTable();
    using (DbDataAdapter adapter = new SqlDataAdapter())
    {
        adapter.SelectCommand = _context.Database.Connection.CreateCommand();
        adapter.SelectCommand.Parameters.Clear();
        adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
        adapter.SelectCommand.CommandText = "GetEvaluations";

        SqlParameter aid = new SqlParameter("@AgentId", SqlDbType.Int);
        aid.Value = AgentId;
        SqlParameter sd = new SqlParameter("@StartDate", StartDate.Date.ToString("dd.MM.yyyy"));
        SqlParameter ed = new SqlParameter("@EndDate", EndDate.Date.ToString("MM.dd.yyyy"));
        SqlParameter fid = new SqlParameter("@FormId", SqlDbType.Int);
        fid.Value = FormId;

        adapter.SelectCommand.Parameters.Add(aid);
        adapter.SelectCommand.Parameters.Add(sd);
        adapter.SelectCommand.Parameters.Add(ed);
        adapter.SelectCommand.Parameters.Add(fid);

        adapter.Fill(table);
    }

    return table;
}

这里的日期时间格式不同,但就是这样工作的。即使这 2 个参数也在寻找 table.

中的相同字段

请查看日期时间格式(它们不同)我认为它们必须是相同的样式。

我的SP:

USE [EvaluationAssistt]
GO
/****** Object:  StoredProcedure [dbo].[GetEvaluations]    Script Date: 12.03.2015 11:25:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Hammit Yildirim
-- Create date: 11.03.2015
-- Description: Defined for Category Point Reports Performance
-- =============================================
ALTER PROCEDURE [dbo].[GetEvaluations]-- 258,'1.11.2014','11.24.2014',8
@AgentId int,
@StartDate Datetime,
@EndDate Datetime,
@FormId int
AS
BEGIN
    SET NOCOUNT ON;

    select fce.Id from FormsCallsEvaluated fce
        left join FormsCalls fc on fce.CallId = fc.CallId
        where fc.AgentId=@AgentId and fc.DateStarted > @StartDate and fc.DateStarted < @EndDate and fce.FormId=@FormId
END

我的问题是什么

我在下面尝试过,结果是空的 table

 public virtual object GetEvaluations(int AgentId, DateTime StartDate, DateTime EndDate, int FormId)
    {
        DataTable table = new DataTable();
        using (DbDataAdapter adapter = new SqlDataAdapter())
        {
            adapter.SelectCommand = _context.Database.Connection.CreateCommand();
            adapter.SelectCommand.Parameters.Clear();
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.SelectCommand.CommandText = "GetEvaluations";

            SqlParameter aid = new SqlParameter("@AgentId", SqlDbType.Int);
            aid.SqlValue = AgentId;
            SqlParameter sd = new SqlParameter("@StartDate", SqlDbType.DateTime);
            sd.SqlValue = StartDate.Date;
            SqlParameter ed = new SqlParameter("@EndDate", SqlDbType.DateTime);
            ed.SqlValue = EndDate.Date;
            SqlParameter fid = new SqlParameter("@FormId", SqlDbType.Int);
            fid.SqlValue = FormId;

            adapter.SelectCommand.Parameters.Add(aid);
            adapter.SelectCommand.Parameters.Add(sd);
            adapter.SelectCommand.Parameters.Add(ed);
            adapter.SelectCommand.Parameters.Add(fid);

            adapter.Fill(table);
        }

        return table;
    }

它正在SQL这边工作

USE [EvaluationAssistt]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[GetEvaluations]
        @AgentId = 258,
        @StartDate = N'1.11.2014',
        @EndDate = N'11.24.2014',
        @FormId = 8

SELECT  'Return Value' = @return_value

GO

您将 @StartDate@EndDate 列定义为 DateTime 但您尝试向它们传递字符串值。

只需将正确的类型和值传递给您的参数,例如;

SqlParameter sd = new SqlParameter("@StartDate", StartDate.Date);
SqlParameter ed = new SqlParameter("@EndDate", EndDate.Date);