I am using Entity Framework and WebAPI with my AngularJS project. As of now the whole table in the database gets converted to JSON, but I really only need 3 columns. Are there any easy way to fix this?
UPDATE : Here is the automatically generated controller using Entity Framework. The table contains columns with project information like "ProjectID", "Project name", "Customer name" etc. I only want a couple of them, let's say "ProjectID" and "Project name".
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 RetGet;
namespace RetGet.controllers
{
public class ProjectsController : ApiController
{
private censoredname db = new censoredname();
// GET: api/Projects
public IQueryable<Project> GetProject()
{
return db.Project;
}
// GET: api/Projects/5
[ResponseType(typeof(Project))]
public IHttpActionResult GetProject(int id)
{
Project project = db.Project.Find(id);
if (project == null)
{
return NotFound();
}
return Ok(project);
}
// PUT: api/Projects/5
[ResponseType(typeof(void))]
public IHttpActionResult PutProject(int id, Project project)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != project.ProjectId)
{
return BadRequest();
}
db.Entry(project).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProjectExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Projects
[ResponseType(typeof(Project))]
public IHttpActionResult PostProject(Project project)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Project.Add(project);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = project.ProjectId }, project);
}
// DELETE: api/Projects/5
[ResponseType(typeof(Project))]
public IHttpActionResult DeleteProject(int id)
{
Project project = db.Project.Find(id);
if (project == null)
{
return NotFound();
}
db.Project.Remove(project);
db.SaveChanges();
return Ok(project);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool ProjectExists(int id)
{
return db.Project.Count(e => e.ProjectId == id) > 0;
}
}
}