How to call Stored Procedures with EntityFramework?

前端 未结 7 1284
青春惊慌失措
青春惊慌失措 2020-12-01 02:03

I have generated an EF4 Model from a MySQL database and I have included both StoredProcedures and Tables.

I know how to make regular instert/update/fetch/delete oper

相关标签:
7条回答
  • 2020-12-01 02:49

    This is an example of querying MySQL procedure using Entity Framework

    This is the definition of my Stored Procedure in MySQL:

    CREATE PROCEDURE GetUpdatedAds (
        IN curChangeTracker BIGINT
        IN PageSize INT
    ) 
    BEGIN
       -- select some recored...
    END;
    

    And this is how I query it using Entity Framework:

     var curChangeTracker = new SqlParameter("@curChangeTracker", MySqlDbType.Int64).Value = 0;
     var pageSize = new SqlParameter("@PageSize", (MySqlDbType.Int64)).Value = 100;
    
     var res = _context.Database.SqlQuery<MyEntityType>($"call GetUpdatedAds({curChangeTracker}, {pageSize})");
    

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

    0 讨论(0)
提交回复
热议问题