Here is my error message: 'IEnumerable' does not contain a definition for 'FirstOrDefaultAsync' and no accessible extension method 'FirstOrDefaultAsync' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)
I have done the following and the error persists:
I did not forget to use await, as per this link: Task does not contain a definition for 'FirstOrDefault'
I referenced Sytem.Core and imported both System.Linq and System.Data.Entity.(System.Linq is ambiguous/grey so it is not being used), as per this link: DbSet doesn't contain definition for FirstOrDefault?
Here's my .cshtml.cs Code, where the error is being generated (line 34):
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Movies.Models;
namespace Movies.Pages.MediaDetails
{
public class DetailsModel : PageModel
{
private readonly Movies.Models.MoviesContext _context;
public DetailsModel(Movies.Models.MoviesContext context)
{
_context = context;
}
MovieDataAccessLayer objmovie = new MovieDataAccessLayer();
public Movies Movies { get; set; }
public async Task<IActionResult> OnGetAsync(byte? id)
{
if (id == null)
{
return NotFound();
}
Movies = await objmovie.MovieByMedia(id).FirstOrDefaultAsync(m => m.MediaTypeID == id);
if (Movies == null)
{
return NotFound();
}
return Page();
}
}
}
Here's my DataAccessLayer, which is being reference by the code above and returns IEnumerable.
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Movies.Models
{
public class MovieDataAccessLayer
{
string connectionString = "Server=servername;Database=DatabaseMovies;Trusted_Connection=True;MultipleActiveResultSets=true";
public IEnumerable<Movies> MovieByMedia(byte? MediaID)
{
List<Movies> lstmovie = new List<Movies>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("usp_MovieByMedia", con);
cmd.Parameters.Add(new SqlParameter("@MediaID", SqlDbType.TinyInt) { Value = MediaID });
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Movies movies = new Movies();
movies.MovieTitle = rdr["MovieTitle"].ToString();
movies.MovieYear = Convert.ToInt32(rdr["MovieYear"]);
lstmovie.Add(movies);
}
con.Close();
}
return lstmovie;
}
}
}
Any input would be appreciated.