This project has moved and is read-only. For the latest updates, please go here.

DbFunctions with Effort and EF6

Nov 4, 2014 at 5:01 PM
I have an extension method defined as follows:
[DbFunction("Project.Data.Model", "StringLike")]
public static bool Like(this string input, string pattern)
    //This will only throw an exception if method is called outside of a Linq query
    throw new NotImplementedException();
When called in a Linq-to-Entities predicate, this method resolves to the following function defined in my EF6 model (edmx file):
<Function Name="StringLike" ReturnType="Boolean">
          <Parameter Name="input" Type="String" />
          <Parameter Name="pattern" Type="String" />
            input LIKE pattern
I am wondering how this function is resolved when called via Effort in a unit test. There are no exceptions thrown, however, the behavior is inconsistent with how it works when using a "real" SQL database context. Namely, the following test does not return the correct results:
var singleCharWildcardEndMatches = TestDbContext.UserProfile.Where(a => a.FirstName.Like("FirstName_"));
The line above actually produces the same results as Like("FirstName%") produces. I would expect the single underscore to match exactly one character, i.e. FirstName9 would match, FirstName987 would not.

Am I correct in assuming there is no actual SQL being performed by effort? If so, what is being done behind the scenes when my extension method is called? What level of support does Effort have for model-defined DbFunctions?
Nov 10, 2014 at 10:40 PM
Upon further investigation, this appears to be a bug with NMemory. NMemory has an explicit implementation of SQL Like using Regex.
The following line appears to be the culprit:
pattern = pattern.Replace("%", ".*?").Replace("_", ".");
When the underscore is at the beginning or end of the input string, the single dot in the Regex pattern is too greedy.
Nov 13, 2014 at 9:02 PM

Thank you for reporting this issue.

This is indeed a bug in NMemory, ^ and $ should be appended to the regex.
I am also wondering what regex mode should be used.
Nov 13, 2014 at 9:03 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.