Click here to Skip to main content
15,892,737 members

The underlying provider failed on an open while using stored procedure in code first

Rohit Kesharwani asked:

Open original thread
Hi,
I am trying to retrieve data from stored procedure in entity framework code first model. But while I try it gives me an error “The underlying provider failed on an open” for the inner list of data.
C#
       using (ProductDbContext dbUserContext = new ProductDbContext())
       {
           listProducts = dbUserContext.Database.SqlQuery<products>("SP_GetAllProducts");
           dbUserContext.Database.Initialize(force: false);

           var dbProdCmd = dbUserContext.Database.Connection.CreateCommand();
           dbProdCmd.Connection = dbUserContext.Database.Connection;

//((IObjectContextAdapter)dbUserContext).ObjectContext.Connection.Open();

           dbProdCmd.CommandText = "SP_GetAllProducts";
           dbProdCmd.CommandType = CommandType.StoredProcedure;
           try
           {
               // dbUserContext.Database.Connection.Open();
               ((IObjectContextAdapter)dbUserContext).ObjectContext.Connection.Open();

               dbUserContext.Database.SqlQuery<products>("SP_GetAllProducts");

               var prodReader = dbProdCmd.ExecuteReader();

               listProducts = ((IObjectContextAdapter)dbUserContext)
              .ObjectContext
              .Translate<products>(prodReader, "Products", MergeOption.AppendOnly).ToList();
               prodReader.Dispose();
           }
           finally
           {
               //dbUserContext.Database.Connection.Close();
               //((SqlConnection)dbUserContext.Database.Connection).Close();

               ((IObjectContextAdapter)dbUserContext).ObjectContext.Connection.Close();
           }
           //((IObjectContextAdapter)dbUserContext).ObjectContext.ContextOptions.LazyLoadingEnabled = false;
           return View(listProducts);
       }

Stored Procedure:
SQL
CREATE PROCEDURE [dbo].[SP_GetAllProducts]
	-- Add the parameters for the stored procedure here
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT OFF;

    -- Insert statements for procedure here
	SELECT * FROM [DBO].[Products]
	SELECT * FROM [DBO].[prd1]
	SELECT * FROM [DBO].[prd2]
	SELECT * FROM [DBO].[prd3]
	SELECT * FROM [DBO].[prd4]
	SELECT * FROM [DBO].[prd5]
	SELECT * FROM [DBO].[prd6]
END

Model: Model structure is something like this:
C#
using System.Collections.Generic;
using System;
using System.Data.Entity;

namespace Project.Models
{
    public class Products
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime CreationDate { get; set; }
        public DateTime ModificationDate { get; set; }

        public virtual ICollection<t> prd1 { get; set; }
        public virtual ICollection<t> prd2 { get; set; }
        public virtual ICollection<t> prd3 { get; set; }
        public virtual ICollection<t> prd4 { get; set; }
        public virtual ICollection<t> prd5 { get; set; }
    }

    public class ProductContext : DbContext
    {
        public DbSet<t> prd { get; set; }
        public DbSet<t> prd1 { get; set; }

        public DbSet<t> prd2 { get; set; }
        public DbSet<t> prd3 { get; set; }
        public DbSet<t> prd4 { get; set; }
        public DbSet<t> prd5 { get; set; }
}

Please reply ASAP.
Thanks.

[Edit]Code block added[/Edit]
Tags: MVC, Entity Framework, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900