Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:00:48+00:00 2026-05-31T22:00:48+00:00

I’m trying to call a stored procedure that accepts a table value parameter. I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:00:49+00:00Added an answer on May 31, 2026 at 10:00 pm

    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:

    class Program
    {
        static void Main(string[] args)
        {
            var entities = new NewBusinessEntities();
    
            var dt = new DataTable();
            dt.Columns.Add("WarningCode");
            dt.Columns.Add("StatusID");
            dt.Columns.Add("DecisionID");
            dt.Columns.Add("Criticality");
    
            dt.Rows.Add("EO01", 9, 4, 0);
            dt.Rows.Add("EO00", 9, 4, 0);
            dt.Rows.Add("EO02", 9, 4, 0);
    
            var caseId = new SqlParameter("caseid", SqlDbType.Int);
            caseId.Value = 1;
    
            var userId = new SqlParameter("userid", SqlDbType.UniqueIdentifier);
            userId.Value = Guid.Parse("846454D9-DE72-4EF4-ABE2-16EC3710EA0F");
    
            var warnings = new SqlParameter("warnings", SqlDbType.Structured);
            warnings.Value= dt;
            warnings.TypeName = "dbo.udt_Warnings";
    
            entities.ExecuteStoredProcedure("usp_RaiseWarnings_rs", userId, warnings, caseId);
        }
    }
    
    public static class ObjectContextExt
    {
        public static void ExecuteStoredProcedure(this ObjectContext context, string storedProcName, params object[] parameters)
        {
            string command = "EXEC " + storedProcName + " @caseid, @userid, @warnings";
    
            context.ExecuteStoreCommand(command, parameters);
        }
    }
    

    and the stored procedure looks like this:

    ALTER PROCEDURE [dbo].[usp_RaiseWarnings_rs]
        (@CaseID int, 
         @UserID uniqueidentifier = '846454D9-DE72-4EF4-ABE2-16EC3710EA0F', --Admin
         @Warnings dbo.udt_Warnings READONLY
    )
    AS
    

    and the user-defined table looks like this:

    CREATE TYPE [dbo].[udt_Warnings] AS TABLE(
        [WarningCode] [nvarchar](5) NULL,
        [StatusID] [int] NULL,
        [DecisionID] [int] NULL,
        [Criticality] [int] NULL DEFAULT ((0))
    )
    

    Constraints I found include:

    1. The parameters you pass into ExecuteStoreCommand have to be in order with the parameters in your stored procedure
    2. You have to pass every column in to your user-defined table, even if they are have defaults. So it seems i couldn’t have a IDENTITY(1,1) NOT NULL column on my UDT
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.