I'm trying to call a stored procedure that accepts a table value parameter. I know that this isn't directly supported in Entity Framework yet but from what I understand you can do it using the ExecuteStoreQuery
command off of the ObjectContext
. I have a generic entity framework repository where I have the following ExecuteStoredProcedure
method:
public IEnumerable<T> ExecuteStoredProcedure<T>(string procedureName, params object[] parameters)
{
StringBuilder command = new StringBuilder();
command.Append("EXEC ");
command.Append(procedureName);
command.Append(" ");
// Add a placeholder for each parameter passed in
for (int i = 0; i < parameters.Length; i++)
{
if (i > 0)
command.Append(",");
command.Append("{" + i + "}");
}
return this.context.ExecuteStoreQuery<T>(command.ToString(), parameters);
}
The command string ends up like this:
EXEC someStoredProcedureName {0},{1},{2},{3},{4},{5},{6},{7}
I tried to run this method on a stored procedure that accepts a table valued parameter and it breaks. I read here that the parameters needed to be of type SqlParameter
and the table valued parameter needs to have the SqlDbType
set to Structured
. So I did this and I get an error stating:
The table type parameter p6 must have a valid type name
So, I set the SqlParameter.TypeName to the name of the user defined type I created on the database and then when I run the query I get the following truly helpful error:
Incorrect syntax near '0'.
I can get the query to run if I revert back to ADO.NET and and execute a data reader but I was hoping to get it to work using the data context.
Is there a way to pass a table value parameter using ExecuteStoreQuery
? Also, I am actually using Entity Framework Code First and casting the DbContext
to an ObjectContext
to get the ExecuteStoreQuery
method available. Is this necessary or can I do this against the DbContext
as well?
I want to share my solution on this problem:
I have stored procedures with several table value parameters and I found out that if you call it this way:
you get a list with no records.
But I played with it more and this line gave me an idea:
I changed my parameter @SomeParameter with its actual value 'SomeParameterValue' in command text. And it worked :) This means that if we have something else than SqlDbType.Structured in our parameters it doesn't pass them all correctly and we get nothing. We need to replace actual parameters with their values.
So, my solution looks as follows:
The code surely could be more optimized but I hope this will help.
The DataTable approach is the only way, but constructing a DataTable and populating it manually is fugly. I wanted to define my DataTable directly from my IEnumerable in a style similar to EF's fluent model builder thingy. So:
I've posted the thing on dontnetfiddle: https://dotnetfiddle.net/ZdpYM3 (note that you can't run it there because not all of the assemblies are loaded into the fiddle)
Change your string concatenation code to produce something like:
Okay, so here is a 2018 update: end to end solution which describes how to invoke stored procedure with table parameter from Entity Framework without nuget packages
I'm using EF 6.xx, SQL Server 2012 and VS2017
1. Your Table Value prameter
Let's say you have a simple table type defined like this (just one column)
2. Your Stored procedure
and a stored procedure with several parameters like:
3. SQL Code to use this stored procedure
In SQL you would use something like that:
4. C# Code to use this stored procedure
And here is how you can call that Stored Procedure from Entity Framework (inside WebAPI):
I hope it helps!
UPDATE
I've added support for this on Nuget Package - https://github.com/Fodsuk/EntityFrameworkExtras#nuget (EF4,EF5,EF6)
Check out the GitHub repository for code examples.
Slightly off question, but none the less useful for people trying to pass user-defined tables into a stored procedure. After playing around with Nick's example and other Stackoverflow posts, I came up with this:
and the stored procedure looks like this:
and the user-defined table looks like this:
Constraints I found include:
ExecuteStoreCommand
have to be in order with the parameters in your stored procedure