【发布时间】:2017-03-04 16:33:24
【问题描述】:
型号:
public class Word
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime? WhenCreated { get; set; }
public ApplicationUser Author { get; set; }
[NotMapped]
public string AuthorName
{
get
{
if (Author != null)
{
return Author.UserName;
}
else {
return "";
}
}
}
public List<Definition> Definitions { get; set; }
}
控制器:
[HttpGet]
public IEnumerable<Word> Get()
{
return _db.Words.Include(x=>x.Author).ToList();
}
我的控制器现在返回整个 ApplicationUser 类,这是 Word 的属性之一。我只想发送ApplicationUser 的一个属性:UserName。我该怎么做?
我添加了AuthorName,它将只返回我想要从ApplicationUser 获得的数据。不幸的是,我仍然需要.Include(x=>x.Author) 才能使该属性正常工作。我可以在数据序列化过程中以某种方式省略包含Author(在向用户发送数据时隐藏它)吗?
我知道我可以使用.Select() 方法,但它需要我输入我需要的所有属性。如果我以后修改我的模型,我将需要更新所有那些.Select(),这会很不方便,而且会浪费时间。
你会怎么解决这个问题?
【问题讨论】:
标签: c# asp.net entity-framework asp.net-core entity-framework-core