First, in EF4.1, create a stored procedure with an output parameter.

ef-code-first entity-framework parameters stored-procedures

Question

Entity Framework 4.1 Code First is what I use. In addition to the strongly typed result set, I want to call a stored method that contains an output parameter and extract the value of that output parameter. The search feature has a signature that looks like this.

public IEnumerable<MyType> Search(int maxRows, out int totalRows, string searchTerm) { ... }

I discovered a ton of "Function Imports" tips, however they are incompatible with Code First. Using a database, I may invoke stored procedures. However, SqlQuery(...) does not function with output arguments.

Can I at anyhow address that issue with EF4.1 Code First?

1
11
11/18/2011 9:31:21 AM

Accepted Answer

SqlQuery uses output parameters, but you must properly create and build your SQL query.SqlParameter Try an approach like:

var outParam = new SqlParameter();
outParam.ParameterName = "TotalRows";
outParam.SqlDbType = SqlDbType.Int;
outParam.ParameterDirection = ParameterDirection.Output;

var data = dbContext.Database.SqlQuery<MyType>("sp_search @SearchTerm, @MaxRows, @TotalRows OUT", 
               new SqlParameter("SearchTerm", searchTerm), 
               new SqlParameter("MaxRows", maxRows),
               outParam);
var result = data.ToList();
totalRows = (int)outParam.Value;
26
11/18/2011 12:42:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow