c# - 意外错误 : LINQ to Entities does not recognize the method 'System.String DecryptValue(Byte[], System.String)' method

标签 c# sql entity-framework linq

我的udf:

[EdmFunction("Model.Store", "Decrypt")]
public static string Decrypt(byte[] Value, string Passphrase)
{
    throw new NotSupportedException("Direct calls are not supported.");
}

我的 LINQ 调用:

var passphrase = "phrase123";
var decryptedValue = (from p in unitOfWork.context.Values
                      where p.ValueID== valueId
                      select Decrypt(p.value, passphrase)).FirstOrDefault();

这是实际的错误消息:

Unexpected Error : LINQ to Entities does not recognize the method 'System.String Decrypt(Byte[], System.String)' method, and this method cannot be translated into a store expression.

SQL函数

CREATE FUNCTION DB.Decrypt
(
    @Value VARBINARY(200),
    @Passphrase varchar(1000)
)
RETURNS VARCHAR(1000)

我该如何解决这个问题才能解密该值?谢谢

更新: 我更改了 edmfunction 以尝试将 varbinary 处理为:

public static string Decrypt(SqlBinary Value, string Passphrase)

并收到一条新的错误消息:

LINQ to Entities does not recognize the method 'System.String Decrypt(System.Data.SqlTypes.SqlBinary, System.String)' method, and this method cannot be translated into a store expression.

这仍然让我相信 C# 中的 varbinary 等价物有问题

最佳答案

Linq to Entities 正在尝试扩展到类似的内容;

select Decrypt(p.Value, @passphrase) 
from values p
where p.valueid = @valueId

并且似乎无法将代码的 Decrypt 函数与服务器上的匹配函数进行匹配。从我见过的其他例子来看,它看起来不错,所以我怀疑这是一个小“粘合”问题......

我想知道这些是否可能是有用的线索;

  • C# Decrypt() 将 byte[] 作为第一个参数——这是 p.value 的实际数据类型吗?例如,如果它是一个字符串,您可能需要微调函数声明中的类型。
  • 您确实在当前连接的数据库上获得了该功能吗?也许缺少某种更新?

关于c# - 意外错误 : LINQ to Entities does not recognize the method 'System.String DecryptValue(Byte[], System.String)' method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37168703/

相关文章:

c# - TextBoxFor 呈现为带有 ID 属性前缀的 HTML

c# - 迁移 : No DbContext was found in assembly

c# - 无法使用 Entity Framework 6 获取存储过程结果

c# - Entity Framework 查询ToString不会产生SQL查询

c# - 从 WPF 中的 ListBox 获取被点击的元素

c# - 如何在 MVC 4 中创建图像按钮

c# - 限项 list

sql - 从存储过程结果集中插入/更新表上的数据

MYSQL查询对某些列的出现次数进行求和

sql - SQL Server 中的左外连接