& # 39; System.Linq.IQueryable< YuClone.Models.video> & # 39

本文关键字:Models YuClone video Linq System IQueryable | 更新日期: 2023-09-27 18:13:46

尝试创建Web API控制器,但当我编译解决方案时,我得到这个错误:

"来。IQueryable'不包含定义'Add'和不接受first的扩展方法'Add'类型为System.Linq的参数。这个IQueryable"查找(您是否丢失了using指令或程序集)参考?)C:'Users'Ahmed'Documents'Visual工作室

同样,'Find'和'Remove'也没有找到。

我的控制器布局如下:

    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;
        }
    }
}

我在这里遗漏了哪个参考?

编辑:我的上下文类如下:

    namespace YuClone.Models
{
    public class YuCloneContext : DbContext
    {
        public YuCloneContext() : base("name=YuCloneContext")
        {
        }
        public IQueryable<video> videos { get; set; }
    }
}

& # 39; System.Linq.IQueryable< YuClone.Models.video> & # 39

将YuCloneContext视频从IQueryable更改为DbSet。DbSet继承了IQueryable,但也包含了一个Add方法