I have a client object that has a computed value Fullname that I want stored to the database but don't want/need it returned from the database because it is computed from other fields.
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);
}
In this case, I want it in the database so reporting users can use the fullname for printing/listing without having to compute it. But I don't need it brought back into my domain model because it's always computed from Firstname and Lastname which are already being brought in from the database.
Is it possible to define a "store to database only" property on an Entity with EF Core?
The solution from the EF Core team on GitHub was as follows:
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 { }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With