简体   繁体   English

使用EF.Functions.Contains()的单元测试方法

[英]Unit Test method with EF.Functions.Contains()

I have got a method which contains a call to EF.Functions.Contains . 我有一个方法,其中包含对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. 现在我想用InMemory数据库为这个方法编写单元测试,但我立即得到以下异常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? 我知道抛出这个异常是因为我的InMemory数据库上没有全文索引与生产的SQL Server实例相比,但是如何在InMemory数据库中获得相同的索引?

Is there any way to get arround this exception? 有没有办法解决这个例外?

Like you said, EF.Functions.Contains can not be used in an InMemory database. 就像你说的, EF.Functions.Contains不能在InMemory数据库中使用。

A workaround is to use IsSqlServer() to check whether you are running against a SQL Server or InMemory provider. 解决方法是使用IsSqlServer()来检查您是针对SQL Server还是InMemory提供程序运行。

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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