Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the entity framework with a MySQL DB and the model designer doesn't pickup stored proc parameters

I've got the latest Mysql connector that allows you to use the Visual Studio Entity Framework designer. It's been working great, but I just added a stored proc.

The Server Explorer loaded it up fine with the parameters specified, but then I added it to the Entity Model & the code it generates doesn't have any input parameters.

Here's the stored procedure

CREATE PROCEDURE `GetViewableMenuNodes`(IN siteId INT, IN parentId INT, IN userName varchar(255))
BEGIN
    select m.*
    from menunode m 
    where m.siteid = siteId
    and m.showinmenu = 1
    and m.parentid = parentId
    and m.viewername = userName;
END

and this is the code generated by the model

public global::System.Data.Objects.ObjectResult<MenuNode> GetViewableMenuNodes()
{
    return base.ExecuteFunction<MenuNode>("GetViewableMenuNodes");
}
like image 803
Glenn Slaven Avatar asked Jun 18 '09 06:06

Glenn Slaven


People also ask

Can Entity Framework be used with MySQL?

MySQL Connector/NET is compatible with multiple versions of Entity Framework Core. For specific compatibility information, see Table 7.2, “Connector/NET Versions and Entity Framework Core Support”.

Can we use stored procedure in Entity Framework?

You can use stored procedures either to get the data or to add/update/delete the records for one or multiple database tables. EF API creates a function instead of an entity in EDM for each stored procedure and User-Defined Function (UDF) in the target database.


1 Answers

In case you find this helpful, here is the approach I use for working with stored procedures with parameters in MySQL from the MySQL Connector/.NET Entity Framework provider. I call ExecuteStoreQuery(). This liberates me from having to deal with the challenges of mapping procedures with parameters in the model. This works for our needs.

    public IList<SearchResultsMember> SearchMembers(int memberID, string countryCode, string regionCode, string cityCode, float distanceKm,
        int genderID, int ageMin, int ageMax, int offsetRowIndex, int maxRows)
    {


        MySqlParameter[] queryParams = new MySqlParameter[] { 
                                        new MySqlParameter("memberIDParam", memberID),
                                        new MySqlParameter("countryCodeParam", countryCode),
                                        new MySqlParameter("regionCodeParam", regionCode),
                                        new MySqlParameter("cityCodeParam", cityCode),
                                        new MySqlParameter("distanceKmParam", distanceKm),
                                        new MySqlParameter("genderIDParam", genderID),
                                        new MySqlParameter("ageMinParam", ageMin),
                                        new MySqlParameter("ageMaxParam", ageMax),
                                        new MySqlParameter("offsetRowIndexParam", offsetRowIndex),
                                        new MySqlParameter("maxRowsParam", maxRows)
                                    };

        StringBuilder sb = new StringBuilder();
        sb.Append("CALL search_members(@memberIDParam, @countryCodeParam, @regionCodeParam, @cityCodeParam, @distanceKmParam, @genderIDParam, @ageMinParam, @ageMaxParam, @offsetRowIndexParam, @maxRowsParam)");

        string commandText = sb.ToString();
        var results = _context.ExecuteStoreQuery<SearchResultsMember>(commandText, queryParams);

        return results.ToList();
    }
like image 144
MomentSurfer Avatar answered Sep 30 '22 17:09

MomentSurfer