I am new in using stored procedure using Microsoft Entity Framework. To do some practice I used Northwind database and I was successfully able to do something like this:
var qry = (from row in dbContext.CustOrderHist("custID").AsEnumerable()
select row).Skip(10).Take(10);
Then I have my own DB I am working with it also has a few stored procedures and I similarly used the 'Add Function Import; feature of .Net to create ComplexType to get the stored procedure results (the way I practiced in Northwind Model).
But when I try to do the same:
var qry = (from row in _dbContext.spComplianceReport("SomeID", null).AsEnumerable()
select row).Skip(10).Take(10);
It would not work, and gives error about AsEnumerable(). The following error:
System.Data.Objects.ObjectResult
does not contain a definition for AsEnumerable and no extension method
AsEnumerable accepting a first argument of type
System.Data.Objects.ObjectResult
could be found (are you missing a using directive or an assembly
reference?)
This problem is with my every stored procedure in my Model. What can be different with my Model than the Northwind.
Ok doing the following resolved my problem:
using System.Linq;
Related
I am running an ASP.NET MVC 5 application, using Entity Framework 6.
My stored procedure is sp_caw_pocs which accepts one parameter and I am accessing the stored procedure in my controller's action method like this:
var query = db.sp_caw_pocs(id).ToList();
Currently, upon a click event, I am using this stored procedure in an action method with a parameter (Integer), and my query result sometimes returns only 1 or sometimes, 3 items are returned.
I am returning them as a partial result to my view:
return PartialView("_CAWPocs", query);
The problem I am facing is in my view, where the returned query result, I am not able to iterate over the collection. It is giving the squiggly under the Model item.
My view:
#model List<PWBMS.DAL.sp_caw_pocs_Result>
Here's the error:
and the error says
List<sp_caw_pocs_Result> does not contain a definition for 'FirstName' and no accessible extension method 'FirstNName' accepting a first argument of type 'List<sp_caw_pocs_Result>' could be found. (are you missing a using directive or assembly reference?)
I am not sure what model I should be using in order to get the result from my ActionResult to iterate over them in the view.
What am I doing wrong? Or is there a better way to call the stored procedure in my controller, to pass the list to my view to iterate over?
TIA.
I have a stored procedure that gets many input parameters (the procedure persoms Insert statement)
I use EF to access this procedure.
Is there a way to automatically generate object type that contains all the input parameters of the procedure?
Something like the complex type that is being generated for the output of the procedure.
My EF version is 6.1.3
No, I don't think it's possible to automatically generate a class for the input parameters of a stored procedure in EF 6.1.3.
This article helped me in working with insert stored procedure with the entity framework:
http://www.entityframeworktutorial.net/EntityFramework5/CRUD-using-stored-procedures.aspx
I have created a stored procedure which returns columns dynamically , but i'm unable to import it into entity framework
Does anybody know how to import a stored procedure that returns columns dynamically
EF doesn't support stored procedure with dynamic result set. The result set must be fixed for EF to map it to complex type / entity type.
I have the code below (this is actually part of a much more complicated query, but I have isolated the issue to this particular line to help with debugging) which per everything I have read should create an IN clause in SQL, assuming I am using EF4. As far as I can tell, I am using EF4 (We are using .NET Framework 4 for our projects and when I look at the System.Data and System.Data.Entity they both say version 4.0.0.0 for all the projects)
int[] assessmentIDs; // this is just here to show what this is,
// but it is a params parameter passed to this methed
var assessments = from cert in container.ProctorAssessmentCertifications
where assessmentIDs.Contains(cert.AssessmentID)
select cert.ID;
However, when I run this, I get the runtime error:
LINQ to Entities does not recognize the method 'Boolean Contains[Int32](Int32[], Int32)' method, and this method cannot be translated into a store expression.
When I use LinqPad, it does correctly output an IN clause like one would expect in EF4.
My questions are:
A. What am I doing wrong and how do I make this work?
B. How do I force EF4 to be called if in fact it's not? I can find no reference in any web.config file that point it to the older version.
Contains does not get translated into valid SQL because assessmentIDs is not IQueryable, it is an in-memory object. So you'll have to pull the data out first, and then do the check.
var assessments = (from cert in container.ProctorAssessmentCertifications
select cert.ID).ToList() //no longer IQueryable.
var result = assessments.Intersect(assessmentIDs);
We are using ado.net entity framework 4.0 for our database layer and I am a newbie to ado.net entity framework. I have created entity via adding a entity in entity framework. I want to map that entity with stored procedure only not table of the database. Stored procedure will return same column as entity. How it is possible and how i can do that without mapping to table?
Here is a complete walkthrough http://msdn.microsoft.com/en-us/library/cc716679.aspx
Its not possible because an ObjectSet is an IQueryable and mapping an ObjectSet to stored procedure would not give u an IQueryable because stored procedures by their very nature cannot be composed. The best you can do is take the content inside the stored procedure and put into a view and map the view to an ObjectSet which is possible.
You need to create a complex type, not an entity. Open up the model browser and import your stored procedure as a "function import" (your SP must not use #tempTables but you can use #tableVariables instead); in the function import wizard you'll see a "create complex type" button.
The SP becomes a method in the model context and you can use it to get IEnumerable[TheComplexType].
In EF4.1 code-first it's even simpler, you put a [ComplexType] attribute on top of any class and you can use that type as a return type for context.ExecuteStoreQuery[T]. If your properties are named exactly as the returned columns are (and the types line up), the mapping is "magic" - it just works.