Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First Computed Properties

I'm using Entity Framework "Code First" approach in a ASP.NET MVC 3 web application. In my database I have several computed columns. I need to use these columns to display data (which works fine).

However when I come to insert rows into this table, I'm getting the following error:

The column "ChargePointText" cannot be modified because it is either a computed column or is the result of a UNION operator.

Is there a way to mark the property as readonly in my class?

public class Tariff
{
    public int TariffId { get; set; }
    public int Seq { get; set; }
    public int TariffType { get; set; }
    public int TariffValue { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int ChargePoint { get; set; }
    public string ChargePointText { get; set; }
}
like image 565
Sam Huggill Avatar asked May 16 '11 09:05

Sam Huggill


1 Answers

I've found the solution. Entity Framework provides a data annotation called DatabaseGenerated. Use it like this:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string ChargePointText { get; set; }

Get more details here: http://msdn.microsoft.com/en-us/library/gg193958.aspx

like image 173
Sam Huggill Avatar answered Oct 21 '22 14:10

Sam Huggill