【发布时间】:2021-04-13 05:33:51
【问题描述】:
我在一个项目中使用 EF Core 5,我的一个实体包含一个 NotMapped 属性,该属性混合了实体的两个属性,我希望在 select 语句中只有包含在 select 语句中的属性从数据库加载但在分析之后,我已经看到所有属性都已加载。
作为示例,Contact 实体包含一个 NotMapped 属性,如下所示。
public class Contact
{
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[NotMapped]
public string FullName => $"{FirstName} {LastName}";
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}
public class SampleContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
}
在下面的查询中,我只需要 ContactId 和 FullName,我希望在 TSQL 查询中只加载 ContactId、FirstName、LastName,但所有属性都已加载。
var list = dbContext.Contacts.Select( e => new
{
e.ContactId,
e.FullName
}).ToList();
【问题讨论】:
标签: c# entity-framework .net-core entity-framework-core