首页 > 解决方案 > Npgsql: What does `AddEntityFrameworkNpgsqlTrigrams` do?

问题描述

Since Ngpsql docs doesn't have a comperhensive page for fuzzy string matching, I went with what test cases are using, and got the following error:

Exception has occurred: CLR/System.AggregateException
An unhandled exception of type 'System.AggregateException' occurred in API.dll: 'Some services are not able to be constructed'
    Inner exceptions found, see $exception in variables window for more details.
    Innermost exception      System.InvalidOperationException : Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMappingSource' while attempting to activate 'Npgsql.EntityFrameworkCore.           PostgreSQL.Query.ExpressionTranslators.Internal.NpgsqlTrigramsMethodCallTranslatorPlugin'.
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean            throwIfCallSiteNotFound)
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)

Normally I'd ask what's happening, but I've noticed that if I don't use AddEntityFrameworkNpgsqlTrigrams, I'm able to compile and my call to a trigram function (TrigramsSimilarity to be exact) results in my expression being translated successfully. So my question is: What is AddEntityFrameworkNpgsqlTrigrams for? Can I leave it out?
I should note that I'm using Npgsql.EntityFrameworkCore.PostgreSQL.Trigrams version 3.1.4. We haven't migrated to .NET 5.0

标签: asp.netentity-framework-corenpgsql

解决方案


使用Trigram插件的常用方法是参考Npgsql.EntityFrameworkCore.PostgreSQL.Trigrams nuget 包,并在您的:OnModelConfiguring

options.UseNpgsql(@"<connection string>", o => o.UseTrigrams())

这在此处记录。应用程序很少需要直接调用 AddEntityFrameworkNpgsqlTrigrams。


推荐阅读