Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EF Core provide a way to map a get-only property to database

Tags:

ef-core-2.1

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?

like image 799
RHarris Avatar asked Jan 24 '26 16:01

RHarris


1 Answers

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 { }
}
like image 172
RHarris Avatar answered Jan 27 '26 12:01

RHarris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!