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