0
试图创建Web API控制器的定义,但是当我编译解决方案,我得到这个错误:“System.Linq.IQueryable <YuClone.Models.video>”不包含“添加”
“ System.Linq.IQueryable”不包含 定义‘添加’,没有扩展方法‘添加’接受型的第一 参数‘System.Linq.IQueryable’可能 找到(是否缺少using指令或程序)C:\ Users \ Ahmed \ Documents \ Visual Studio
同样找不到'查找'和'删除'。
我的控制器布局为以下几点:
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; }
}
}
显示YuCloneContext'是如何定义'。 – MarcinJuraszek
我应该将其更改为公共Dbset
是的。这将解决您的问题。 – MarcinJuraszek