I'm calling a SQL stored procedure in EF
var paramSortColumn = new SqlParameter("@SortColumn", SqlDbType.NVarChar, 200) { Value = SortColumn };
var paramSortOrder = new SqlParameter("@SortOrder", SqlDbType.NVarChar, 10) { Value = SortOrder };
var paramStartIndex = new SqlParameter("@StartIndex", SqlDbType.Int) { Value = filter.StartIndex - 1 };
var paramItemsPerPage = new SqlParameter("@ItemsPerPage", SqlDbType.Int) { Value = filter.ItemsPerPage };
var paramDealerBranchID = new SqlParameter("@DealerBranchIDs", SqlDbType.NVarChar){Value = selectedIds };
var paramBypassDealerBranchIDs = new SqlParameter("@BypassDealerBranchIDs", SqlDbType.TinyInt) { Value = Convert.ToInt32(filter.ByPassDealerBranchIDs) };
var result =
_dbContext.Database.SqlQuery<DealerMappingDetailsReportModel>(
"usp_DealerMappingDetail @DealerBranchIDs,@BypassDealerBranchIDs,@SortColumn,@SortOrder,@StartIndex,@ItemsPerPage",
paramDealerBranchID, paramBypassDealerBranchIDs, paramSortColumn, paramSortOrder, paramStartIndex, paramItemsPerPage
);
return result.ToList();
but it returns
The SqlParameter is already contained by another SqlParameterCollection
I had the same issue with a Database.SqlQuery
that was calling a Stored Procedure
. Many folks recommend "clearing" and/or "cloning" the Parameters
collection (among other things).
However...
I noticed my SQL command-text wasn't "complete"...and found...I had forgotten to include the EXEC
command in my SQL. It looks like you may have the same issue.
I CHANGED THIS:
In my methods, I usually like to hand-back Queryables, so my methods often look like below.
...notice that 'EXEC' is missing
var query = UnitOfWork.DbContext.Database.SqlQuery<DocumentStatusDataItem>("dbo.usp_ListDocumentStatusForATFDocuments @ContextFullName, @WorkflowNames",
new SqlParameter("ContextFullName", context.FullName),
new SqlParameter("WorkflowNames", namesXML))
.AsQueryable();
TO THIS:
...notice that 'EXEC' is now there
var query = UnitOfWork.DbContext.Database.SqlQuery<DocumentStatusDataItem>("EXEC dbo.usp_ListDocumentStatusForATFDocuments @ContextFullName, @WorkflowNames",
new SqlParameter("ContextFullName", context.FullName),
new SqlParameter("WorkflowNames", namesXML))
.AsQueryable();
Then my calls started working:
For example...calls like this suddenly started working...and I stopped getting the exception.
List<DocumentStatusDataItem> collection = query.ToList();
However, this same solution also worked for ToList()
methods I have that were having the same issue.
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.