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.

Simon Williams
  • 1,016
  • 3
  • 11
  • 27

2 Answers2

10

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);
    }
}
tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • 1
    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? – Simon Williams Apr 05 '13 at 10:30
  • @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: http://stackoverflow.com/questions/5691780/navigation-property-without-declaring-foreign-key – tpeczek Apr 05 '13 at 10:54
  • I have solved my problem by splitting my classes into two as in this article http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx I have one class that contains the computed field and another class that contains any fields that I need to be able to edit. – Simon Williams Apr 05 '13 at 19:24
3

Mark property as computed:

modelBuilder
    .Entity<MyEntityType>()
    .Property(_ => _.MyProperty)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • 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? – Simon Williams Apr 05 '13 at 10:13
  • @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? – Dennis Apr 05 '13 at 10:54
  • 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. – Simon Williams Apr 05 '13 at 12:37