title: Encrypt/Decrypt column in sql server using entity framework
body: <p>I am trying to encrypt and decrypt some columns in SQL database using Entity Framework 6 and database project. </p>
  
  <p>I created my master key:</p>
  
  <pre><code>CREATE COLUMN MASTER KEY [CMK_1]
  WITH
  (
      KEY_STORE_PROVIDER_NAME = N'MSSQL_CERTIFICATE_STORE',
      KEY_PATH = N'CurrentUser/my/D4331582F1485517DA1807CF0A5500C........'
  )
  </code></pre>
  
  <p>and my encryption key:</p>
  
  <pre><code>CREATE COLUMN ENCRYPTION KEY [CEK_1]
  WITH VALUES
  (
       COLUMN_MASTER_KEY = [CMK_1],
       ALGORITHM = N'RSA_OAEP',
       ENCRYPTED_VALUE = 0x016E00000163007500....
  );
  </code></pre>
  
  <p>and mt table is: </p>
  
  <pre><code>CREATE TABLE [dbo].[MyWords]
  (
      [Id] INT NOT NULL PRIMARY KEY IDENTITY,
      [MyWord] [NVARCHAR](4) COLLATE Latin1_General_BIN2 ENCRYPTED WITH(ENCRYPTION_TYPE = DETERMINISTIC, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = [CEK_1]) NOT NULL,
  )
  GO
  </code></pre>
  
  <p>The database is created successfully. Both the table and the keys exist.</p>
  
  <p>I am trying to add a new entry to the database using entity framework but I am getting this error: </p>
  
  <blockquote>
    <p>Failed to decrypt a column cipher key using the following key store
    provider: 'MSSQL_CERTIFICATE_STORE'. The last 10 bytes of the
    encrypted column encryption key are: 'AF-B3-97-27-5A-BC-33-24-75-FA'.
    \ R \ nDegumental certificate
    'D4331582F1485517DA1807C.....' was not in the 'My'
    certificate store under the location 'CurrentUser' found.</p>
  </blockquote>
  
  <p>I don't know why. </p>
  
  <p>P.S. I added the enable parameter in the connection string: </p>
  
  <blockquote>
    <p>Column Encryption Setting = Enabled;</p>
  </blockquote>
  
  <p>Am I missing something here? Why do I receive this error on .SaveChanges?
  a little explanation would be very helpful!
  Thank you!</p>