tshoemake
tshoemake

Reputation: 1351

Unexpected Error : LINQ to Entities does not recognize the method 'System.String DecryptValue(Byte[], System.String)' method

My udf:

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

My LINQ call:

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

This is the actual error message:

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 function

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

How do i fix this in order to decrypt the value? Thanks

UPDATE: I changed my edmfunction to try to handle the varbinary to this:

public static string Decrypt(SqlBinary Value, string Passphrase)

And got a new error message of this:

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.

Which still leads me to believe something is wrong with the varbinary equvialent in C#

Upvotes: 2

Views: 775

Answers (2)

tshoemake
tshoemake

Reputation: 1351

The answer was so simple. My EdmFunction was using the wrong reference due to resharper's advice.... the correct class to be using is

using System.Data.Entity.Core.Objects.DataClasses;

Sorry for bringing the headache to you all, thanks for all of your efforts!

Upvotes: 1

Steve Cooper
Steve Cooper

Reputation: 21480

Linq to Entities is trying to expand to something like;

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

and it seems to be having trouble matching your code's Decrypt function with the matching one on the server. It looks ok, from other examples I've seen, so I suspect it's a minor 'glue' issue...

I wonder if any of these might be useful leads;

  • C# Decrypt() takes a byte[] as the first parameter -- is that the actual datatype of p.value? If it's a string, for instance, you might need to nudge the types in the function declaration.
  • Have you definitely got the function on the database you're currently connecting to? Maybe missing some kind of update?

Upvotes: 1

Related Questions