【问题标题】:EntityFramework Procedure or function '' expects parameter '', which was not suppliedEntityFramework 过程或函数“”需要参数“”,但未提供
【发布时间】:2010-03-22 15:30:14
【问题描述】:

我很抱歉只问了一个基本问题,但是我找不到这个错误的原因。

我正在使用实体框架执行存储过程,并传入四个参数,但是 SQL 数据库似乎拒绝了它们。谁能指出我正确的方向?

我的代码:

ObjectResult<SearchDirectoryItem> resultList = container.ExecuteStoreQuery<SearchDirectoryItem>("SearchDirectoryEntries",
            new SqlParameter("@DirectoryId", search.DirectoryId),
            new SqlParameter("@Latitude", point.Latitude),
            new SqlParameter("@Longitude", point.Longitude),
            new SqlParameter("@Range", search.RangeMiles));

产生错误的原因:

过程或函数“SearchDirectoryEntries”需要参数“@DirectoryId”,但未提供。

生成的SQL是:

exec sp_executesql N'SearchDirectoryEntries',N'@DirectoryId int,@Latitude decimal(7,5),@Longitude decimal(6,5),@Range int',@DirectoryId=3,@Latitude=53.36993,@Longitude=-2.37013,@Range=10

存储过程是:

ALTER PROCEDURE [dbo].[SearchDirectoryEntries]
@DirectoryId int,
@Latitude decimal(18, 6),
@Longitude decimal(18, 6),
@Range int

非常感谢。

【问题讨论】:

  • 你能发布你的SQL存储过程的声明吗?到哪里定义参数?
  • ALTER PROCEDURE [dbo].[SearchDirectoryEntries] @DirectoryId int, @Latitude decimal(18, 6), @Longitude decimal(18, 6), @Range int 干杯
  • searchpoint的定义是什么?即 search.DirectoryIdint 吗?
  • 没有一个值是空的,它们都传递给SQL: exec sp_executesql N'SearchDirectoryEntries',N'@DirectoryId int,@Latitude decimal(7,5),@Longitude decimal(6, 5),@Range int',@DirectoryId=3,@Latitude=53.36993,@Longitude=-2.37013,@Range=10

标签: c# sql entity-framework


【解决方案1】:

您查询中的 commandText 参数不正确。它应该是对带有参数的存储过程的调用,而不仅仅是存储过程名称:

ObjectResult<SearchDirectoryItem> resultList = container.ExecuteStoreQuery<SearchDirectoryItem>(
    "Exec SearchDirectoryEntries @DirectoryId, @Latitude, @Longitude, @Range",
     new SqlParameter("DirectoryId", search.DirectoryId),
     new SqlParameter("Latitude", point.Latitude),
     new SqlParameter("Longitude", point.Longitude),
     new SqlParameter("Range", search.RangeMiles));

另外不要忘记从 SqlParameter 构造函数中删除“@”。

【讨论】:

  • 谢谢!在无数次错误开始后完全解决了我的问题。
  • SqlParameter 的文档中没有任何地方指定从名称中删除 @。那是从哪里来的?但是,如果您使用的是 ObjectParameter,则必须将其删除。
猜你喜欢
  • 1970-01-01
  • 2013-09-09
  • 2020-12-14
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多