stored procedure with EF 4.1 - entity-framework

I have a parameterized stored procedure which is executing a view and return the results. The view is showing results of join of two tables. I need to pass parameters to this stored procedure and call it from MVC3 controller action using EF 4.1 code first approach and return the results. How can I do this. Please suggest step by step.
Thanks.

I don't know what part of "Code First doesn't support stored procedures" people don't understand, but this keeps coming up and it shouldn't. You can muddle your way around it (kind of), but really if you need to execute stored procedures then you shouldn't be using code first at this point.

You might check this post as well. It might help with at least the first step: executing a proc with parameters in EF 4.1
How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure? EF Code First CTP5
My Code:
context.Database.SqlQuery<EntityType>(
"EXEC ProcName #param1, #param2",
new SqlParameter("param1", param1),
new SqlParameter("param2", param2));

I'm not sure if this will help you but here's how I call an Oracle proc using EF 4.1 and the DevArt driver. This just returns a single value but you may be able to adapt it to return multiple rows. I have a package.proc called P_SID.SID_PGet:
PROCEDURE SID_PGet(io_SID OUT varchar2) is
Begin
io_SID:=GetSID;
End;
Below is how I call it and retrieve the SID value:
var parameter = new Devart.Data.Oracle.OracleParameter("io_SID", Devart.Data.Oracle.OracleDbType.VarChar, ParameterDirection.Output);
this.Database.ExecuteSqlCommand("BEGIN P_SID.SID_PGet(:io_SID); END;", parameter);
var sid = parameter.Value as string;
return sid;

Related

EF Core stored procedure with 2 parameters error System.InvalidOperationException:

I am moving to EF Core and trying to call a stored procedure with 2 parameters.
The current setup for 1 parameter works fine.
Here is the error I see:
System.InvalidOperationException: FromSqlRaw or FromSqlInterpolated was called with non-compostable SQL and with a query composing over it. Consider calling AsEnumerable after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client-side. at
Here is my repository call:
public IQueryable<PartsGridDTO> GetPartsGridQueryable(int partId, int groupId)
{
return MoreContext.SpPartsGridDetailYourCases
.FromSqlRaw($"EXECUTE dbo.GetProductsByPartAndGroup {partId}, {groupId}")
.Select(s => new PartsGridDTO
{
PartId = partId,
//....
}).AsQueryable();
}
I've tried some fixes from here:
Include with FromSqlRaw and stored procedure in EF Core 3.1
but none of those work.
I am returning AsQueryable() up until the controller and then calling .ToListAsync().
I tried adding .IgnoreQueryFilters(); right before .AsQueryable(), but I still see the error.
Any thoughts? Am I passing in the parameters wrong? Is it an EF Core issue or?
There were 3 items until I resolved the issue:
I had extra properties that were not within the return of the stored procedure. I guess that causes the error I posted above.
(A sub-issue) I also only needed to return items that the grid was going to display. Meaning the grid might show only 5 of the 8 values that are returned within the stored procedure. Having those extra columns in the return items of stored procedure called caused this error later: InvalidOperationException: The required column 'Address' was not present in the results of a 'FromSql' operation
Here is the final look of my stored proc code:
return Context.EntityExample.FromSqlRaw($"dbo.GetProductsByPartAndGroup {partId}, {groupId}")

C# Entity Framework - How to generate object type for input parameters of stored procedure

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

Stored Procedure without mapping with DbSet

Can we call stored procedure without mapping with DbSet using Entity Framework Code First approach
Thanks
You can use raw SQL and call the procedure that way.
var blogs = context.Blogs.SqlQuery("dbo.GetBlogs").ToList();
or
var blogs = context.ExecuteSqlCommand("dbo.GetBlogs").ToList();

Updateing a Table with a Stored Procedure or Functions in Catel

I've set up a database with EF6 and Code First and I am using Catel's Repository Pattern to communicate with the database.
Now, I am curious as to how you would call a stored procedure (or even a Scalar Valued Function) on my database using Catel. Any suggestions?
You should still be able to call the stored procedure using the DbContext itself as you usually would:
using (var dbContextManager = DbContextManager<MyEntities>.GetManager())
{
var dbContext = dbContextManager.DbContext;
var result = dbContext.Database.SqlQuery<ReturnType>("storedProcedureName", params);
}
https://msdn.microsoft.com/en-us/library/gg696545(v=vs.113).aspx

LINQ to Entities (.NET 4.0)

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);