简体   繁体   中英

EntityFramework Procedure or function '' expects parameter '', which was not supplied

I apologise for asking just a basic question, however I cannot find the cause of this error.

I am using Entity Framework to execute a Stored Procedure, and I am passing in four parameters, however the SQL Database seems to reject them. Can anyone point me in the right direction?

My code:

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));

Which produces the error:

Procedure or function 'SearchDirectoryEntries' expects parameter '@DirectoryId', which was not supplied.

The SQL generated is:

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

The stored procedures is:

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

Many Thanks.

The commandText param in your query is incorrect. It should be a call to a stored procedure with parameters instead of just stored procedure name:

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));

Also don't forget to remove '@' from SqlParameter constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM