简体   繁体   中英

Unit Test method with EF.Functions.Contains()

I have got a method which contains a call to EF.Functions.Contains . Now I want to write unit tests for this method with a InMemory database, but I am getting instantly the following exception System.InvalidOperationException : The 'Contains' method is not supported because the query has switched to client-evaluation.

My method looks like this

var attributeValues = Context.AssetAttributeValues
                                         .Include(a => a.AssetAttribute)
                                         .Include(a => a.Asset)
                                         .Where(i => EF.Functions.Contains(i.Value, searchString));

I know that this exception is thrown because I have got no fulltext index on my InMemory database compared to my productive SQL Server instance but how do I get the same index on the InMemory database?

Is there any way to get arround this exception?

Like you said, EF.Functions.Contains can not be used in an InMemory database.

A workaround is to use IsSqlServer() to check whether you are running against a SQL Server or InMemory provider.

if(Context.IsSqlServer())  
    return Context.AssetAttributeValues
                                         .Include(a => a.AssetAttribute)
                                         .Include(a => a.Asset)
                                         .Where(i => EF.Functions.Contains(i.Value, searchString));
else
    return Context.AssetAttributeValues
                                         .Include(a => a.AssetAttribute)
                                         .Include(a => a.Asset)
                                         .Where(i => i.Value.Contains(searchString));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM