0
votes

Trying to create Web API controller but I get this error when I compile the solution:

'System.Linq.IQueryable' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?) C:\Users\Ahmed\Documents\Visual Studio

Similarly 'Find' and 'Remove' are also not found.

My controller is laid out as the following:

    using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using YuClone.Models;

namespace YuClone.Controllers
{
    public class videosController : ApiController
    {
        private YuCloneContext db = new YuCloneContext();

        // GET: api/videos
        public IQueryable<video> Getvideos()
        {
            return db.videos;
        }

        // GET: api/videos/5
        [ResponseType(typeof(video))]
        public IHttpActionResult Getvideo(long id)
        {
            video video = db.videos.Find(id);
            if (video == null)
            {
                return NotFound();
            }

            return Ok(video);
        }

        // PUT: api/videos/5
        [ResponseType(typeof(void))]
        public IHttpActionResult Putvideo(long id, video video)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != video.videoid)
            {
                return BadRequest();
            }

            db.Entry(video).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!videoExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/videos
        [ResponseType(typeof(video))]
        public IHttpActionResult Postvideo(video video)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.videos.Add(video);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = video.videoid }, video);
        }

        // DELETE: api/videos/5
        [ResponseType(typeof(video))]
        public IHttpActionResult Deletevideo(long id)
        {
            video video = db.videos.Find(id);
            if (video == null)
            {
                return NotFound();
            }

            db.videos.Remove(video);
            db.SaveChanges();

            return Ok(video);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool videoExists(long id)
        {
            return db.videos.Count(e => e.videoid == id) > 0;
        }
    }
}

Which reference am I missing here?

Edit: My context class is as following:

    namespace YuClone.Models
{
    public class YuCloneContext : DbContext
    {

        public YuCloneContext() : base("name=YuCloneContext")
        {

        }

        public IQueryable<video> videos { get; set; }
    }
}
1
Show how YuCloneContext is defined.MarcinJuraszek
I should change it to public Dbset<video> videos {get; set;} shouldn't I?Ahmed Mujtaba
Yes. That will solve your issue.MarcinJuraszek

1 Answers

0
votes

Change YuCloneContext videos from IQueryable to DbSet. DbSet inherits from IQueryable but also includes an Add method.