This code works as expected: the ExhibitionName property brings the concatenation of the fields according to their values.
public partial class Client
{
public int Id { get; set; }
public int Reference { get; set; }
[StringLength(100)]
public string Name{ get; set; } = null!;
[StringLength(50)]
public string? Nickname{ get; set; }
public string ExhibitionName => (!String.IsNullOrWhiteSpace(Nickname) ? Nickname: Name) + " (" + Reference+ ")";
public DateOnly Added { get; set; }
// other properties
}
// db is the DbContext instance for Client.
db.Clients.Select(c => new { c.Id, c.ExhibitionName }).ToList();
However, the SQL statement generated by Entity Framework brings all the fields from the table, even the ones not related, which is not desirable.
The second option would be to use HasComputedColumnSql in the model mapping and achieve the same result via the database.
Is there a third option?