简体   繁体   中英

Execute stored procedure using entity framework

is it possible to execute a stored procedure using EF, that select records from database from two or more tables using inner join and left outer join.

my point of view is to avoid the method of doing joins in EF or LINQ, which i have many issues with it.

so if i create that procedure, can i call it with parameters from user input, can assign the result to .ToList() method and then add the result to asp:repeater .DataSource .

I know it may be a strange question, but i want to do this for many reasons first, to use EF because i feel more comfortable. second, to get rid of using joins in EF. third, i read somewhere that using stored procedure will enhance query performance, when used to call a query frequently.

if any one could help me to answer these questions with an example i would be appreciate.

You can call SqlQuery from your Entity Framework data context.

context.Database.SqlQuery<YourType>("exec usp_StoredProcedure").ToList()

You would need a class to map the query results back, as an example:

public class YourType
{
   public string Property1 { get; set; }
   public string Property2 { get; set; }
}

You can also specify parameters to the query as shown below:

SqlParameter parameter1 = new SqlParameter("@Parameter1", "Value");
context.Database.SqlQuery<YourType>("exec usp_StoredProcedure @Parameter1", parameter1).ToList()

We are going to see how to execute the stored procedure in Entity Framework, in MVC we are going to see the how to add the EF.

Execute the following script in the database to create a stored procedure.

CREATE PROCEDURE FETCHEMPLOYEES AS
BEGIN
    SELECT * FROM EMPTABLE
END

CREATE PROCEDURE FETCHEMPLOYEE(@ID INT) AS
BEGIN
  SELECT * FROM EMPTABLE WHERE ID = @ID
END



public class EmpModel
{
    EmployeeEntities empdb = new EmployeeEntities();

    public List<EMPTABLE> GetEmployees()
    {
       return empdb.FETCHEMPLOYEES().ToList();  
    }

    public EMPTABLE GetEmployee(int? id)
    {
        return empdb.FETCHEMPLOYEE(id).ToList().Single();
    }
}

public class EmployeeController : Controller
{
    Models.EmpModel mod = new Models.EmpModel();

    public ActionResult Index()
    {
        List<EMPTABLE> result = mod.GetEmployees();
        return View(result);
    }

    public ActionResult Details(int id)
    {
        EMPTABLE result = mod.GetEmployee(id);
        return View(result);
    }
}

For more step by step details, please refer following link: http://dotnetvisio.blogspot.in/2014/01/execute-stored-procedure-using-entity.html

you can use ExecuteFunction of ObjectContext class:

 public virtual ObjectResult<CustomClassForStoreReturnedData> NameOfStoredProcedure(Nullable<int> tableID)
    {
        var IDParameter = tableID.HasValue ?
            new ObjectParameter("tableID", tableID) :
            new ObjectParameter("tableID", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<CustomClassForStoreReturnedData>("NameOfStoredProcedureInSQLDatabase", IDParameter);
    }

In you are using Entity Framework with MySQL:

In this example, my stored procedure is expecting one parameter, named myParam of type BIGINT

var myParam = new SqlParameter("@myParam", (MySqlDbType.Int64)).Value = 100;

var res = _context.Database.SqlQuery<MyEntity>($"call MyProcedureName({pyParam})");

Note that I am using C# String Interpolation to build my query string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM