Entity Framework Code First Computed Properties
Asked Answered
C

2

20

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; }
}
Coriolanus answered 16/5, 2011 at 9:18 Comment(1)
Yes you are right - my answer was incorrect. It doesn't work only if you want EF to generate database. Post your update as answer and accept it.Otoole
C
38

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

Coriolanus answered 16/5, 2011 at 12:50 Comment(1)
Were you able to get this to work for UPDATEs ? In particular for a "DateTime" property for say a 'LastUpdatedOn' attribute of your model class?Liaoning
A
3

There are a couple of other options as well.

The first is to change the property setup in the mapping file from:

this.Property(t => t.Name)
    .HasMaxLength(152);

to:

this.Property(t => t.Name)
    .HasMaxLength(152)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

This is pretty much the same as Same Huggill's solution, except it keeps this configuration in the mapping, rather than in the model. I feel this is slightly better since the mapping class already contains code to tell Entity Framework how to load that type of entity, so the knowledge that a field is computed belongs in there.

Another option is the NotMappedAttribute which can be applied to individual properties of an entity like so:

public class User
{
    ...

    [NotMapped]
    public string Name
    {
        get;
        set;
    }

    ...
}

This is useful for when the entity contains properties that are not populated from the database, but it should be equally useful in the scenario faced by OP, i.e., EF will not try to push the value in a property marked with the NotMappedAttribute, therefore insert should work.

Acrilan answered 20/6, 2013 at 23:18 Comment(2)
He is trying to use computed database fields. "NotMapped" will not help him do this.Vacillation
Yes. I did. Using the not mapped attribute will not help him to display db calculated fields within his app unless he wants to also calculate them in his app.Vacillation

© 2022 - 2024 — McMap. All rights reserved.