I am using Entity Framework Code First, and I have an entity defined with a StartTime
property, an EndTime
property and a Duration
pro
Inspired by Slauma's answer, I was able to achieve what I was aiming for by using a protected setter. This persisted the value back to the database but didn't allow the value to be modified elsewhere.
My property now looks like this:
public int Duration
{
get
{
return (int)this.EndTime.Subtract(this.StartTime).TotalMinutes;
}
protected set {}
}
Supplying an empty setter might be a possible solution (although then the property isn't readonly anymore, of course):
public int Duration
{
get
{
return (int)this.EndTime.Subtract(this.StartTime).TotalMinutes;
}
set { }
}
As far as I know, readonly properties are not mappable to a column in the database.