实体框架: 存储过程或函数''期望参数'',但未提供

12

非常抱歉我提出一个基础问题,但是我找不到这个错误的原因。

我正在使用Entity Framework执行存储过程,并传入了四个参数,然而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存储过程声明吗?包括参数定义的部分。 - Andy Shellam
修改存储过程 [dbo].[SearchDirectoryEntries] @DirectoryId int, @Latitude decimal(18, 6), @Longitude decimal(18, 6), @Range int干杯 - James
“search”和“point”的定义是什么?即search.DirectoryId是一个int吗? - ChrisF
所有的值都不是空值,它们都被传递到 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 - James
1个回答

23

你的查询中的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 构造函数中删除 '@' 符号。


1
谢谢!在多次失败后,我终于完全解决了我的问题。 - strongriley
1
SqlParameter 的文档中没有指定要从名称中删除 @。这是从哪里来的?但是,如果您使用 ObjectParameter,则必须将其删除。 - test

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接