Hi,
I have a model and one of the properties is a calculation based on another property (average age).
Below is just pseudo code and I have not tested it.
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
[DatabaseGenerated(DataGeneratedOption.Computed)]
public string FullName
{
get { return FirstName + " " + LastName; }
private set { }
}
}
public class Students { public int Id { get; set; }
.... public virtual ICollection<Person> People { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int AverageAge { get { return People.Sum(p=>p.Age) / People.Count; } private set {} } }
In another similar example that I tried, the above would generate columns for both FullName and AverageAge, with default values.
Now from my understanding of this line [DatabaseGenerated(DatabaseGeneratedOption.Computed)], the values are re-calculated when a row is added or updated - which is great for my usecase.
My questions:
- If all the values generated via `Identity` or `Computed` are not set in the application, but rather, the database is responsible, then why does the database even create a new column considering these columns only seem to contain identical, default values?
- With the assumption above, what happens if you add the attribute [NotMapped] to those properties? The column would not get generated, but does the calculation still occur?