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 1039793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:08:39+00:00 2026-05-16T15:08:39+00:00

I want to execute stored procedure with one parameter that returns table using EF4

  • 0

I want to execute stored procedure with one parameter that returns table using EF4 “Code First”. I am ok with some DTO just for this purpose, it doesn’t have to return entities. I have tried to:

a) create edmx file with function import and add it to my ObjectContext like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.RegisterEdmx("Model.edmx");
}

but I got InvalidOperationException saying “Registration of an existing model cannot be used after code first configuration using the fluent API has been started.”

b) access underling connection and execute the procedure:

    var connection = this.objectContext.UnderlyingContext.Connection;
    connection.Open();
    DbCommand command = connection.CreateCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "booklog.Recommendations";
    command.Parameters.Add(
        new EntityParameter("userId", DbType.Guid) { Value = this.userId });
    var reader = command.ExecuteReader();
    // etc.

where

public class MyObjectContext : DbContext
{
    public System.Data.Objects.ObjectContext UnderlyingContext
    {
        get { return this.ObjectContext; }
    }

    // ....
 }

but this approach doesn’t work as well. It throws InvalidOperationException with message “The container ‘booklog’ specified for the FunctionImport could not be found in the current workspace.”

  • 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-16T15:08:40+00:00Added an answer on May 16, 2026 at 3:08 pm

    Ok, b) is correct, but one doesn’t have to use UnderlyingContext, just ObjectContext.Database.Connection. Here is the code:

        var connection = this.objectContext.Database.Connection;
        connection.Open();
    
        DbCommand command = connection.CreateCommand();
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "Recommendations";
        command.Parameters.Add(
            new SqlParameter("param", DbType.Guid) { Value = id });
    
        var reader = command.ExecuteReader();
        while (reader.Read())
        {
            // ...
        }
    
        connection.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer var result = new Collection<string>(collection1.Concat(collection2).ToList()); For some reason System.Collections.ObjectModel.Collection requires… May 16, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer You should do something like this with jQuery + JSON… May 16, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer You can do this pretty easily using Team Foundation Build.… May 16, 2026 at 6:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a stored procedure called sp_BulkInsert that inserts one .csv file into my
Is there anyway I can execute stored procedure via EXEC (so not specifying CommandType.StoredProcedure
I have a sql stored procedure that runs for about 3 minutes, I am
In my stored procedure I have to pass a table and column name which
I have a stored procedure that executes a couple of queries. Each query might
I have a very simple stored procedure which returns multiple record sets. All of
I want to programatically create a SQLDataSet in Delphi and use it to execute
If I have a simple table and I want to do something (say ..
In MS SQL Server if I want to check the results from a Stored
I know there have been numerous questions here about inline sql vs stored procedures...

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.