I'm using EF-Code First and this is my model with Data Annotation:
public int Count { get; set; }
public int Fee { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int Total { get; set; }
Or like this:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int Total
{
get { return Fee * Count; }
private set{}
}
When I want to create a new record I'm getting the following error:
Cannot insert the value NULL into column 'Total'; column does not allow nulls. INSERT fails. The statement has been terminated.
As you can see I'm getting the correct computed value in the Create Action Method with model binder:
By removing the DatabaseGenerated(DatabaseGeneratedOption.Computed)
and set the computed value from my code it works fine and I see the updated total in the database. But my question is if I can do this without DatabaseGenerated(DatabaseGeneratedOption.Computed)
so what's the DatabaseGenerated(DatabaseGeneratedOption.Computed)
usage?
This works:
public int Total
{
get { return Fee * Count; }
private set{}
}