【发布时间】:2014-08-01 14:20:08
【问题描述】:
我有一个视图模型
public class MyViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public string Email { get; set; }
public string Office { get; set; }
public DateTime? StartDate { get; set; }
public int? Age { get; set; }
public int? Salary { get; set; }
public int? Extn { get; set; }
}
我正在对我的实体进行投影
public List<ViewModel.StaffViewModel> GetAll()
{
var context = new GistDemoDbEntities();
var model = context.Staff
.Select(s => new ViewModel.StaffViewModel
{
FirstName = s.FirstName,
LastName = s.LastName,
Position = s.Position,
Salary = s.Salary
}).ToList();
return model;
}
并使用 Web Api 以 json 形式返回,但作为响应,我发现它还包括其他属性,这些属性在视图模型中定义为 vlaue null。我只想拥有我需要的那些属性,这怎么可能?
【问题讨论】:
标签: entity-framework-6 asp.net-web-api2