title: EF Code First Encrypt/Decrypt Password Property
body: <p>I have model like below:</p>
  
  <pre><code>public class User 
  {
      private string password;
      public string Password
      {
          get { return Decrypt(password); }
          set { password = Encrypt(value); }
      }
  }
  </code></pre>
  
  <p>I want from user code to use just insert and select password easily and see in clear text and want the logic layer to handle the encryption and decryption. </p>
  
  <p>But when selecting, EF will <code>set</code> the <code>value</code> again so the encrypted password gotten from database will be encrypted again for me to get from the client code which is a problem. I also cannot distinguish between an <code>Insert</code> and <code>Select</code> to set conditionals. </p>
  
  <p>I can very well do this:</p>
  
  <pre><code>//Insert
  user.Password = Encrypt("123"); //Encrypt or Hash does not matter
  context.Users.Add(user);
  
  //Select
  var hashedpassword = context.Users.Find(1).Select(u =&gt; u.Password).Single();
  var unhashed = Decrypt(hashedpassword);
  </code></pre>
  
  <p>Instead I would like it not to be apparent from client code:</p>
  
  <pre><code>//Insert
  user.Password = "123"; //Inserts encrypted password in database
  context.Users.Add(user);
  
  //Select
  var cleartextpassword = context.Users.Find(1).Select(u =&gt; u.Password).Single();
  </code></pre>
  
  <p>Hope I was able to explain the problem and someone can point me to the right direction. If it is even possible or not.</p>