Skip to content

Setting Up and Initializing Column Encryption with MSSQL Always Encrypted #23970

Description

@daniels7

Hello,

I'd love if it would be possible to initialize the Column Encryption from application layer, so that you just add an annotation like [ColumnEncryption] to every column that should be encrypted, so that it is picked up by migrations which build the necessary T-SQL.

For now you have to either do it yourself in SSMS or Powershell or by some complex T-SQL that you have to hack into your migrations.

Greetings,
Daniel

Activity

  1. roji commented on Jan 25, 2021

    @roji
    Member

    This looks like it would involve supporting the ENCRYPTED WITH ... syntax when creating columns. Doing that depends on having an encryption key predefined - we should be very careful with what we provide in this area (if at all).

  2. daniels7 commented on Jan 25, 2021

    @daniels7
    Author

    Hello @roji ,

    thanks for your fast answer :-)
    What would be the problem about that?

    Besides that it would be possible as well to either create new encryption keys, dependent on the Config or let the user define some, etc. It would greatly enhance the convenience of EF Core if it would be possible to include those things in a clean and pre-defined way instead of having to care about that yourself.

  3. roji commented on Jan 25, 2021

    @roji
    Member

    We have to be extremely careful not to have any sensitive data slip into the migrations or model snapshot - that would be a security concern. I'm not familiar enough with SQL Server Always Encrypted to know exactly how the encryption key should be managed, we'd have to carefully research this.

    On the other hand, the CREATE TABLE clause does seem pretty straightforward.

  4. daniels7 commented on Jan 26, 2021

    @daniels7
    Author

    It would be enough to support scenarios where the key is generated automatically by the database or already there. The Powershell-Scripts I use for now to initialize the encryption don't contain any sensitive data so it shouldn't be required.

  5. added this to the Backlog milestone on Jan 26, 2021
  6. roji commented on Feb 24, 2022

    @roji
    Member

    Note that it seems possible to use ALTER TABLE to add Always Encrypted to existing columns (e.g. this). Assuming that's indeed the case, people can use raw SQL in their migrations to set Always Encrypted after their tables/columns are created by EF Core.

  7. mblumenauer commented on Jun 27, 2022

    @mblumenauer

    I would love to have better support for Always Encrypted in EF Core. I want to Unit Test against a real Database since some operators are not supported with an encrypted Database. But i cant figure out a fast and reliable way to create a new Database to use in each of my Unit Tests.

    Altering the Column Encryption after the Column is created is not possible with Raw Sql.
    Using Migrations and changing the AddColumn statement could be possible but using migrations is slower then just calling EnsureCreated.
    Right now im calling a PowerShell script to set up the encryption keys and changing the columns to encrypted. But this is slow too.

    I wish there was a way to add custom Sql to the Model Builder so the columns get created with encryption enabled when i call EnsureCreated.

  8. ajcvickers commented on Jun 29, 2022

    @ajcvickers
    Contributor

    @mblumenauer I filed #28343 as a possible approach for this.

  9. roji commented on Jun 29, 2022

    @roji
    Member

    Using Migrations and changing the AddColumn statement could be possible but using migrations is slower then just calling EnsureCreated.

    Do you mean in terms of development speed, or runtime speed? Because running (minimal) migrations vs. EnsureCreated really shouldn't have any meaningful difference. I'd advise simply using migrations for this kind of case.

  10. fepettersen commented on Jun 2, 2023

    @fepettersen

    Letting people know that I second this (and by extension that someone else cares). I think it would be beneficial to look at this together with the SQL Server tooling team though. As it stands the tooling for SQL Server Always Encrypted is somewhat lacking, and only runs on PowerShell 5.

  11. kandaker commented on Sep 25, 2023

    @kandaker

    +1. I also would love to see this.
    I understand it's possible to insert this into migrations myself. But I don't hand-edit migrations often, so am worried about taking long to learn+debug. I decided not to use Always Encrypted until it is supported with EF CORE code-first. Instead I wrote the code to encrypt/decrypt (in a deterministic way - to enable some EF/DB content-matching queries to still work) whenever writing and reading the affected properties.

  12. roji commented on Sep 25, 2023

    @roji
    Member

    @kandaker modifying/hand-editing migrations is both really easy and also recommended (for cases when it is needed); I'd definitely recommend against using a different encryption method just to avoid doing it.

  13. Ogglas commented on Oct 25, 2023

    @Ogglas

    This would be really good!

  14. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions