我有一个简单的数据库函数,它需要两个字符串作为参数并返回一个字符串。我想用实体框架来映射它。如同这个问题 https://stackoverflow.com/questions/45451383/dbfunction-cannot-be-translated-into-a-linq-to-entities-store-expression,我创建了一个简单的函数头:
[DbFunction("dbo", "StripCharacters")]
public static string StripCharacters(string input, string pattern = "^A-Z0-9") => throw new NotSupportedException();
就像链接的帖子中一样,当我尝试在其中一个查询中使用此方法时,我会收到相同的错误消息。异常消息是:
Method System.String StripCharacters(System.String, System.String)
类型DE.ZA.TrailerLoadingAssistant.Web.Models.DatabaseEntities
无法转换为 LINQ to Entities 存储表达式
await mapi.db.TrailerAutocompleteHelpers
.Where(t => t.SearchString.Contains(DatabaseEntities.StripCharacters(userInput, "^A-Z0-9")))
.ToListAsync();
这是数据库函数:
CREATE FUNCTION [dbo].[StripCharacters]
(
@String NVARCHAR(MAX),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX) WITH SCHEMABINDING
AS
BEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
RETURN @String
END
我怎么解决这个问题?