I am using Entity Framework Code First in my ASP.NET MVC application. One of my classes has several columns that are added together. I am storing these columns as computed columns in the tables by running an alter table script in the database initializer. Let's say the class looks like:
public class Bond
{
public decimal ParAmountOfIssuance { get; set; }
public decimal AccruedInterest { get; set; }
public decimal Premium { get; set; }
public decimal OriginalIssueDiscount { get; set; }
}
The alter script is something like:
alter table Bonds
add TotalSources as (ParAmountOfIssuance + AccruedInterest + Premium - OriginalIssueDiscount)
I want the Total Sources
column to be available for viewing in the web app. What's the best way to accomplish this? The [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
attribute doesn't work because EF Code First creates the table from the class before the alter script is ran.
Any suggestions are welcome.
Computed
attribute on the property in order to let EF handle computed columns correctly (only read, never write). Consequence is that it only works with mapping to existing DBs but not with a DB created by EF Code-First. See also here: msdn.microsoft.com/en-us/library/gg193958.aspx in section "DatabaseGenerated", 2nd paragraph.