将参数传递给存储过程EF6(MapToStoredProcedures)

时间:2017-05-24 19:21:07

标签: asp.net sql-server asp.net-mvc entity-framework linq

我有一个包含两个类似属性的实体,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

0 个答案:

没有答案