改变加密列属性的最佳做法是什么?

时间:2018-04-25 07:24:52

标签: entity-framework always-encrypted

我的[用户]表格,其中[电子邮件]列使用Always-Encrypt加密。

我需要将[电子邮件]的长度从max限制为MaxLength(250),我这样做是通过在电子邮件属性上添加 MaxLength(250)来实现的。

public class User
{    
    [Key, Required]
    public Guid Id { get; set; }
    [Required, **MaxLength(250)**]
    public string Email { get; set; }
    ...
}

但是当我运行迁移脚本时,我遇到了以下异常:

Operand type clash: nvarchar(max) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK1', column_encryption_key_database_name = 'Identity') is incompatible with nvarchar

并且迁移脚本是:

DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.User')
AND col_name(parent_object_id, parent_column_id) = 'Email';
IF @var0 IS NOT NULL
    EXECUTE('ALTER TABLE [dbo].[User] DROP CONSTRAINT [' + @var0 + ']')
ALTER TABLE [dbo].[User] ALTER COLUMN [Email] [nvarchar](250) NOT NULL
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201804250659054_12345678', N'Concordya.PWC.Verify.DataAccess.Migrations.Configuration',  0x1F8... , N'6.2.0-61023')

我在DB中手动运行脚本,同样的错误。

这是否意味着一旦列加密,更改属性的唯一方法是解密,修改,然后加密?

谢谢,

1 个答案:

答案 0 :(得分:1)

尝试增加Always Encrypted NVARCHAR列的大小时遇到​​了类似的错误。问题是ALTER COLUMN语句仍需要包含加密参数。因此,例如,您可以更改此-

ALTER TABLE [dbo].[User] ALTER COLUMN [Email] [nvarchar](250) NOT NULL

对此(或您最初将Always Encrypted参数设置为的任何参数)-

ALTER TABLE [dbo].[User] ALTER COLUMN [Email] [nvarchar](250) NOT NULL
ENCRYPTED WITH (
      ENCRYPTION_TYPE = DETERMINISTIC
    , ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256'
    , COLUMN_ENCRYPTION_KEY = [**YOUR KEY**]
) NULL