我在stackoverflow和google上搜索了解决方案,并找到了一些类似的答案:
EF 6.1 select scalar function result
Entity Framework 6 Code First Custom Functions
https://long2know.com/2015/09/entity-framework-scalar-valued-functions
但是不能满足我的要求,我的情况是:
SQL Server中的标量值函数:
CREATE FUNCTION [dbo].[fnDecrypt]
(
@cipherText varbinary(max),
@key varchar(200)
)
RETURNS nvarchar(max)
AS
BEGIN
declare @returnVal nvarchar(max)
set @returnVal = convert(nvarchar(max), DecryptByPassPhrase(@key, @cipherText))
return @returnVal
END
C#类:
public partial class ContractEntity : BaseEntity
{
//in sql server, this column is encrypted by EncryptByPassPhrase algorithm, the data type is varbinary(max)
public string Buyer { get; set; }
}
我想要的效果:
_contractRepository.GetById(1);
_contractRepository.Table.Where(p => UserFunctions.FnDecrypt(p.Seller, "Key123") == "seller name").ToList(); // example for CodeFirstStoreFunctions, but the data type is different
_contractRepository.Table.Where(p => UserFunctions.FnDecrypt(p.Seller, "Key123").Contains("seller name")).ToList();
select Id, dbo.fnDecrypt(Seller, 'Key123') from table where Id=1;
select Id, dbo.fnDecrypt(Seller, 'Key123') from table where dbo.fnDecrypt(Seller, 'Key123') like '%seller name%';
我想在类的属性上添加属性,将它们映射到数据库列,支持varbinary(max)到字符串转换,以及条件查询。 谢谢!