使用Web API和实体框架选择要从表中序列化为JSON的列?

时间:2014-11-16 11:06:10

标签: json angularjs entity-framework asp.net-web-api json.net

我在我的AngularJS项目中使用Entity Framework和WebAPI。截至目前,数据库中的整个表都转换为JSON,但我真的只需要3列。有没有简单的方法来解决这个问题?

更新:这是使用Entity Framework自动生成的控制器。该表包含项目信息的列,如" ProjectID","项目名称","客户名称"我只想要几个,让我们说" ProjectID"和"项目名称"。

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

}

1 个答案:

答案 0 :(得分:0)

经过大量的谷歌搜索后,似乎在使用EF 6时你必须使用JSON.NET [DataContract]和[DataMember]。见例。

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