0

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

}

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Praise
  • 557
  • 2
  • 8
  • 23
  • 2
    offcourse you can do filtering, rgiht – harishr Nov 16 '14 at 11:21
  • 1
    It might help people answer your question if you add your controller method so we can see what you're doing... – Paul D'Ambra Nov 16 '14 at 20:00
  • @BrianRogers Had trouble relating the code from that answer to mine. Correct me if I am wrong, has posted the code for the controller. – Praise Nov 17 '14 at 08:18
  • @MariusMathisen The question I linked originally doesn't apply here. Originally your question said you were trying to filter your results to only include certain rows. But now that you've added more information to your question, it seems you are trying to exclude columns instead. That is a different problem. However, I still believe this question is a duplicate: see [here](http://stackoverflow.com/q/13588022/10263). – Brian Rogers Nov 17 '14 at 16:32
  • @BrianRogers Thank you, Brian. But I actually got it to work using DataContract and DataMember. See my answer. – Praise Nov 18 '14 at 07:29

1 Answers1

0

After a lot of googling it seems that when using EF 6 you have to use JSON.NET [DataContract] and [DataMember]. See example.

 [DataContract]
public partial class Project
{
    [DataMember]
    public int ProjectId { get; set; }
    public string PROJECT_NUMBER { get; set; }
    public string PROJECT_NAME { get; set; }
    [DataMember]
    public string LONG_NAME { get; set; }
    public decimal CUSTOMER_ID { get; set; }
    public string CUSTOMER_NAME { get; set; }
}
Praise
  • 557
  • 2
  • 8
  • 23
  • `[DataContract]` and `[DataMember]` are not actually part of Json.Net, although they are respected by Json.Net. These attributes are defined by Microsoft in the [`System.Runtime.Serialization` namespace](http://msdn.microsoft.com/en-us/library/System.Runtime.Serialization(v=vs.110).aspx). The Json.Net equivalent attributes are `[JsonObject(MemberSerialization.OptIn)]` and `[JsonProperty]`. – Brian Rogers Nov 18 '14 at 07:39