stored procedure with EF 4.1
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 result开发者_StackOverflow中文版s. 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;
精彩评论