1

In examples from Microsoft, they implicitly defined a calculated property by providing only accessor without mutator:

public string NormalProperty { get; set; }
public string CalculatedProperty
{
    get { return "foobar" + NormalProperty; }
}

I also want to have a mutator in CalculatedProperty, which automatically trims the "foobar" suffix and assigns the result back into NormalProperty.

public string CalculatedProperty
{
    get { return "foobar" + NormalProperty; }
    set { NormalProperty = value.Substring(6); }
}

The issue is, Entity Framework now considers CalculatedProperty a normal property, and create a column named "CalculatedProperty" in the database.

I do not want to workaround with this by using functions. Can it be done through Attributes/Fluent API? I am using EF 6.1.

Livy
  • 631
  • 4
  • 15
  • which mode do you use? Code First, or Database first with edmx file? If it is the latter one, I may have a solution – Michael Mairegger Apr 03 '14 at 08:46
  • I am building a new website from the ground up using Code First. :( – Livy Apr 03 '14 at 08:48
  • possible duplicate of [Ignoring a class property in Entity Framework 4.1 Code First](http://stackoverflow.com/questions/10385248/ignoring-a-class-property-in-entity-framework-4-1-code-first) – Gert Arnold Apr 03 '14 at 08:50

1 Answers1

1

It can be done in your DataContext in code first

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
   modelBuilder.Entity<Your_Class>().Ignore(x => x.CalculatedProperty); 
}
SWilko
  • 3,542
  • 1
  • 16
  • 26
  • I read Gert Arnold's link above and solved the problem. Thanks for your answer anyway. – Livy Apr 03 '14 at 09:49