5

I have a class called Client mapped to a database table using Entity Framework code first. The table has a computed field that I need available in my Client class, but I understand that it won't be possible to write to this field. Is there a way of configuring Entity Framework to ignore the property when saving, but include the property when reading?

I have tried using the Ignore method in my configuration class, or using the [NotMapped] attribute, but these prevent the property from being read from the database.

2 Answers 2

12

You can use DatabaseGeneratedAttribute with DatabaseGeneratedOption.Computed option:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public ComputedPropertyType ComputedProperty { get; set; }

or if you prefer fluent api you can use HasDatabaseGeneratedOption method in your DbContext class:

public class EntitiesContext : DbContext
{
    public DbSet<EntityType> Enities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<EntityType>().Property(e => e.ComputedProperty).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that seems to be the answer. However, since I am using my computed property as a foreign key reference to another table, I am now getting "A dependent property in a ReferentialConstraint is mapped to a store-generated column." Is it no longer going to be possible to use it as a foreign key reference?
@EasyTimer I honestly have hard time to imagine such a scenario. From EF point of view this is not possible because EF needs to be able to update foreign key if you change the reference in the application. Maybe in your case (as I don't know your exact requirements) having just the navigation properties would be enough, take a look here: stackoverflow.com/questions/5691780/…
I have solved my problem by splitting my classes into two as in this article weblogs.asp.net/manavi/archive/2011/04/24/… I have one class that contains the computed field and another class that contains any fields that I need to be able to edit.
4

Mark property as computed:

modelBuilder
    .Entity<MyEntityType>()
    .Property(_ => _.MyProperty)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

3 Comments

Thanks, that seems to be the answer. However, since I am using my computed property as a foreign key reference to another table, I am now getting "A dependent property in a ReferentialConstraint is mapped to a store-generated column." Is it no longer going to be possible to use it as a foreign key reference?
@EasyTimer: computed property as a foreign key? OMG. What would you do, if the computation result will not match any primary key value in primary key table?
I realise it is an unusual and not ideal scenario. As it happens, the primary key table is itself a view whose primary key is computed in the same fashion as the foreign key table and there are unlikely to be any discrepancies. It is all part of introducing new code into a legacy system, where my hands are tied slightly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.