Trying to create Web API controller but I get this error when I compile the solution:
尝试创建Web API控制器,但在编译解决方案时出现此错误:
'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
'System.Linq.IQueryable'不包含'Add'的定义,也没有扩展方法'Add'接受类型为'System.Linq.IQueryable'的第一个参数'(你是否缺少using指令或程序集引用) ?)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 个解决方案
#1
0
Change YuCloneContext videos from IQueryable to DbSet. DbSet inherits from IQueryable but also includes an Add method.
将YuCloneContext视频从IQueryable更改为DbSet。 DbSet继承自IQueryable,但也包含Add方法。
#1
0
Change YuCloneContext videos from IQueryable to DbSet. DbSet inherits from IQueryable but also includes an Add method.
将YuCloneContext视频从IQueryable更改为DbSet。 DbSet继承自IQueryable,但也包含Add方法。