【发布时间】:2018-10-10 04:51:53
【问题描述】:
我在 Sql 2016 中使用 Always Encrypt 选项。我已使用 MVC 应用程序将 SQL 与实体框架连接起来。我已按照以下链接中的说明加密了该列。 https://www.codeproject.com/Articles/1110564/Always-Encrypted-feature-in-SQL-Server 我可以通过实体框架使用插入查询插入数据。但我无法使用存储过程插入数据。
表格详情如下。
create table Team(Id int not null primary key identity, Name nvarchar(100))
注意:如果我们将列长度更改为 nvarchar(max),则存储过程可以正常工作。
我们有一个现有的数据库。我们不应该更改现有数据库中的任何内容。在不影响 GDPR 合规性的现有数据库结构的情况下,使用 Always 加密的最佳方法是什么?
代码示例
存储过程
USE [EncryptTest]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Insert_Team]
@full_name nvarchar(100)
As
BEGIN
INSERT INTO Team (Name)
VALUES (@full_name)
END
工作
using (var cont = new EncryptTestEntities())
{
Teamt = new EncrypTTest.Team();
t.Name = "Melody";
t.Email = "melody@gmail.com";
cont.Teams.Add(t);
cont.SaveChanges();
}
不工作
using (var cont = new EncryptTestEntities())
{
cont.Insert_Team("Melody");
}
通过存储过程插入数据时出错
Operand type clash: nvarchar(4000) encrypted with
(encryption_type = 'DETERMINISTIC', encryption_algorithm_name =
'Algorithm Name', column_encryption_key_name = 'keyName',
column_encryption_key_database_name = 'SampleEncryption') is
incompatible with nvarchar(100) encrypted with (encryption_type =
'DETERMINISTIC', encryption_algorithm_name = 'Algorithm Name',
column_encryption_key_name = 'keyName',
column_encryption_key_database_name = 'SampleEncryption')
【问题讨论】:
-
对,所以您在应用程序中发现了一个错误,目前您允许通过存储过程输入
nvarchar(4000),尽管该表只能容纳nvarchar(100)。现在您正在使用加密,系统无法继续支持这种数据大小的不匹配。如果保留现有错误对您来说很重要,那么您就无法进行加密。否则,您确实需要进行更改,以便不再有错误。当然,走哪条路是你的决定…… -
我无法准确了解您。你能解释一下吗?我的问题是 SP 只抛出异常。但我可以使用插入查询插入
-
您可以使用查询插入,因为您可能没有为插入定义列。引擎会自动隐式推断类型。由于您已将过程输入定义为
nvarchar(100),因此现在明显不正确。
标签: sql sql-server encryption sql-server-2016