Entity Framework 6 provides a mechanism that allows the storage procedure in Code-FIRST applications. This is very useful when you need to use the existing storage procedures with the EF model, or to create your own storage procedure for specific operations.
Call the storage procedure
In the Entity Framework 6, you can use the method on the DBCONTEXT class.
Method accepts two parameters: the name of the storage procedure to be called and the parameter array passed to the stored procedure.
SqlQuery
For example, the following code calls SqlQuery
Storage procedures and pass
insert_department
Name
Back multiple results sets
<code class="language-csharp">this.Database.SqlQuery<int>("insert_department", new SqlParameter("@Name", departmentName));</code>
Some storage procedures return multiple results sets. If you need to access the results of each result set, you can use the method. Method Returns a
, which contains the results of the results of each result set.
ExecuteSqlQuery
For example, the following code calls ExecuteSqlQuery
Storage procedures and retrieve the results in the ObjectResult
object list:
get_departments
Other descriptions Department
<code class="language-csharp">var departments = this.Database.ExecuteSqlQuery<Department>("get_departments");</code>
If the storage procedure returns the scalar value, you can use the method to retrieve the value.
If the storage procedure has output parameters, you can use theExecuteScalar
You can use the method on the method to map the physical class to the storage procedure. This allows you to use the , ExecuteStoredProcedure
DbModelBuilder
The above is the detailed content of How Can I Call Stored Procedures in Entity Framework 6 (Code-First)?. For more information, please follow other related articles on the PHP Chinese website!