We use Database First and the EntityFramework.
The "Map your own function." approach works for us together with the nuget EntityFramework.CodeFirstStoreFunctions.
1 Step: Create a function in the db like this:
CREATE FUNCTION [dbo].[StringLike]
(
@a nvarchar(4000),
@b nvarchar(4000)
)
RETURNS bit
AS
BEGIN
RETURN
(SELECT CASE
WHEN (SELECT 1 WHERE @a LIKE @b) = 1 THEN 1
ELSE 0
END)
END
2 Step: Install nuget EntityFramework.CodeFirstStoreFunctions
3 Step: Create a method in your code like this (I create mine in the DbContext class):
[DbFunction("CodeFirstDatabaseSchema", "StringLike")]
public static bool Like(string input, string pattern)
{
throw new NotSupportedException("Direct calls are not supported.");
}
4 Step: Initalize EntityFramework.CodeFirstStoreFunctions.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new FunctionsConvention("dbo", this.GetType()));
}
5 Step: Now you can use this method in your linq query.