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

前端 未结 7 1892
梦谈多话
梦谈多话 2021-01-13 08:34

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.

7条回答
  •  迷失自我
    2021-01-13 08:55

    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 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(commandText, queryParams);
    
            return results.ToList();
        }
    

提交回复
热议问题