1

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:

  1. I did not forget to use await, as per this link: Task does not contain a definition for 'FirstOrDefault'

  2. 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.

sqlcdr
  • 123
  • 2
  • 8

2 Answers2

6

The .FirstOrDefaultAsync() method is an extension method on the IQueryable interface. Since your data access method returns an IEnumerable and isn't asynchronous, you should be using .FirstOrDefault().

https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.queryableextensions.firstordefaultasync?view=entity-framework-6.2.0

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=netframework-4.8

A-A-ron
  • 529
  • 1
  • 5
  • 14
  • Thanks for the answer and explanation. This removes the error, although my code still isn't working right. – sqlcdr Apr 19 '19 at 13:00
-1

Try converting your IEnumerable into an actual collection of some sort, such as:

objmovie.MovieByMedia(id).ToList().FirstOrDefault()
Steven Frank
  • 551
  • 3
  • 16
  • When i do that it says that List does not contain definition for FirstOrDefaultAsync() – sqlcdr Apr 19 '19 at 03:47
  • I didn't realize that there was no Async at the end of the function. This does get rid of the error, but my code still need some work. Thanks! – sqlcdr Apr 19 '19 at 12:58