如何将实体框架属性映射到标量值函数

时间:2018-07-09 18:28:31

标签: sql sql-server

我在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

https://blog.3d-logic.com/2014/04/09/support-for-store-functions-tvfs-and-stored-procs-in-entity-framework-6-1/

但是不能满足我的要求,我的情况是:

  • 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; }
    }
    
  • 我想要的效果:

    1. 服务层调用存储库层(通用存储库)不修改或很少修改代码
    2. 支持以下类似代码:

_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)到字符串转换,以及条件查询。 谢谢!

0 个答案:

没有答案