我有一个包含两个类似属性的实体,AttributeValue (byte[])
和DecryptedAttributeValue (string)
。 DecryptedAttributeValue
只存储一个字符串,然后将其加密并保存在AttributeValue
中。 我不希望DecryptedAttributeValue
保存在我的数据库中。
我正在尝试使用代码优先在保存操作上调用SPROC,接收所有值以及DecryptedAttributeValue
,加密它并将其保存在AttributeValue列中。但是我在尝试这样做时遇到了很多问题。一个错误如下:
绑定到属性'DecryptedAttributeValue'的参数不是 在修改函数'SubjectAttributeValue_Insert'中找到。 确保该参数对此修改操作有效 它不是数据库生成的。
我的实体
public class SubjectAttributeValue
{
..... // properties removed for simplification
public int SubjectAttributeValueId { get; set; }
public byte[] AttributeValue { get; set; }
[NotMapped]
public string DecryptedAttributeValue { get; set; }
}
我的DbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SubjectAttributeValue>()
.MapToStoredProcedures(
s => { s.Insert(i => i.Parameter(p => p.DecryptedAttributeValue, "DecryptedAttributeValue")); }
);
}
我的SPROC(为简化而删除了其他实体,这不是1列表)
CREATE PROCEDURE [dbo].[SubjectAttributeValue_Insert]
@DecryptedAttributeValue [nvarchar](255)
AS
BEGIN
OPEN SYMMETRIC KEY xx_SymmetricKey
DECRYPTION BY CERTIFICATE xx_Cert
INSERT [dbo].[SubjectAttributeValues]([AttributeValue])
VALUES (EncryptByKey(Key_GUID('GBAR_Cert'),@DecryptedAttributeValue))
CLOSE SYMMETRIC KEY xx_Cert
... // removed for simplification
END