Is it possible to override the getter/setter for EF CodeFirst properties?

Viewed 5476

Is it possible to add logic to the getter/setter for EntityFramework CodeFirst properties?

For example, I'd like to do something like:

public class Dog {
     // This is a normal EF-backed property....
     public virtual string Breed { get; set; }

     // But we'd like a little logic applied to the Name!
     public virtual string Name {
          get {
               string nameInDB = base.Name; 
               // where base.Name would be the same as the naked "get;" accessor

               if (nameInDB == "Fido") {
                   return "Fido the Third"
               } else {
                   return nameInDB;
               }
          }
          set;
     }
}

public class PetContext : DbContext {
    public DbSet<Dog> Dogs;
}

Where the Dog.Name property can be processed a little before returning.

1 Answers
Related