4

I am starting to use Entity Framework 4. assuming I can not change the database fields. I have a field DateAdded that is stored as YYYYMMDD and I would like to have the entity.DateAdded as a DateTime type. It would make it much easier to work with.

Is there a way to do a custom column data type mapping? or any workarounds?

1 Answer 1

1

You could add an additional property in a partial class for the entity that wraps the "raw" property.

namespace TheNamespace
{

  public partial class TheEntity
  {
      public DateTime DateAdded 
      {
         get {  }
         set {  }
      }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, is there a way to "hide" the raw property and only expose the additional one?
+1 I use this method for bit fields that are nullable in the database but need to be mapped to html checkboxes and can't be null in that instance. Works very well.
@everLearningStudent - I believe that you can set the property getter / setter scope in the properties of the entity model. Try setting both of those to private.
Great, that completes the picture. And to preserve the variable name, I called the generated property DateAddedString ( in the model) and kept DateAdded for the property in the partial class. This way it is exposed as the same name as the database with the expected data type. Thank you all.

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.