Hi, @P. G. Choudhury. Welcome to Microsoft Q&A.
Solution:
Just like DbSet
, define your GetAverageRatingForRecipe
in DbContext
Reference code (Assume your DbContext
is MyDbContext
)
public partial class MyDbContext : DbContext
{
public virtual DbSet<Review> Reviews { get; set; }
public int GetRecipeAverageRating(int recipeId)
{
var averageRatingParam = new SqlParameter
{
ParameterName = "@AverageRating",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
};
Database.ExecuteSqlRaw("EXEC dbo.RecipeAverageRating @RecipeId, @AverageRating OUT", new SqlParameter("@RecipeId", recipeId), averageRatingParam);
return (int)averageRatingParam.Value;
}
…
}
Call and get directly in [HttpGet("{recipeId}")]
[HttpGet("{recipeId}")]
public async Task<ActionResult> GetAverageRatingForRecipe(int recipeId)
{
return Ok(_dbContext.GetRecipeAverageRating(recipeId));
}
Assume your table structure and data are as follows: Test Results:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.