EF Core 是否提供了一种将只获取 属性 映射到数据库的方法
Does EF Core provide a way to map a get-only property to database
我有一个客户端对象,它有一个计算值 Fullname
,我想将它存储到数据库中,但不 want/need 它从数据库返回,因为它是从其他字段计算的。
public class Client
{
public int Id {get; set;}
public string Firstname {get; set;}
public string Middlename {get; set;}
public string Lastname {get; set;}
public string Fullname => this.FormatName(NameFormats.LastFirst);
}
在这种情况下,我希望它在数据库中,以便报告用户可以使用 printing/listing 的全名而无需计算它。但我不需要将它带回我的领域模型,因为它总是从 Firstname
和 Lastname
计算得出,它们已经从数据库中带入。
是否可以在具有 EF Core 的实体上定义 "store to database only" 属性?
EF Core 团队 on GitHub 的解决方案如下:
We discussed this in #7946 and decided that having an empty setter is reasonable for this scenario. For example:
public string Fullname
{
get => $"{this.Lastname}, {this.Firstname}";
private set { }
}
我有一个客户端对象,它有一个计算值 Fullname
,我想将它存储到数据库中,但不 want/need 它从数据库返回,因为它是从其他字段计算的。
public class Client
{
public int Id {get; set;}
public string Firstname {get; set;}
public string Middlename {get; set;}
public string Lastname {get; set;}
public string Fullname => this.FormatName(NameFormats.LastFirst);
}
在这种情况下,我希望它在数据库中,以便报告用户可以使用 printing/listing 的全名而无需计算它。但我不需要将它带回我的领域模型,因为它总是从 Firstname
和 Lastname
计算得出,它们已经从数据库中带入。
是否可以在具有 EF Core 的实体上定义 "store to database only" 属性?
EF Core 团队 on GitHub 的解决方案如下:
We discussed this in #7946 and decided that having an empty setter is reasonable for this scenario. For example:
public string Fullname
{
get => $"{this.Lastname}, {this.Firstname}";
private set { }
}