Migrating from ASP.NET CORE 3.0 to 3.1

954 Views Asked by At

I'm trying to migrate my project from asp.net core 3.0 to 3.1 and I have found one issue:

public static bool CheckConnection(this DataContext dataContext)
    {
        try
        {
            return dataContext.Database.CanConnect();
        }
        catch(SqlException)
        {
            return false;
        }
    }

this code worked in version 3.0, but when I have migrated I got this exception:

System.InvalidOperationException HResult=0x80131509 Message=The database provider attempted to register an implementation of the 'IRelationalTypeMappingSource' service. This is not a service defined by EF and as such must be registered as a provider-specific service using the 'TryAddProviderSpecificServices' method. Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.GetServiceCharacteristics(Type serviceType) at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.TryAdd(Type serviceType, Type implementationType) at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.TryAddTService,TImplementation at Microsoft.Extensions.DependencyInjection.SqlServerServiceCollectionExtensions.AddEntityFrameworkSqlServer(IServiceCollection serviceCollection) at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension.ApplyServices(IServiceCollection services) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.g__BuildServiceProvider|3() at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.b__2(Int64 k) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired) at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_Dependencies() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.CanConnect() at FooBar.AppCore.DbSetExtension.CheckConnection(DataContext dataContext) in D:\Work\FooBar\FooBar\AppCore\DbSetExtension.cs:line 331 at FooBar.Startup.ConfigureServices(IServiceCollection services) in D:\Work\FooBar\FooBar\Startup.cs:line 68 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.b__0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.b__0(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at FooBar.Program.Main(String[] args) in D:\Work\FooBar\FooBar\Program.cs:line 17

Am I missing something or it is common problem and I have to change my code?

Thanks for any suggestion.

2

There are 2 best solutions below

0
On BEST ANSWER

As indicated in this github issue, this can occur when you have different versions of EF Core in your project. Verify that all projects in your solution refer to the same version of EF Core.

0
On

In my case it is LinqKit v 5.0.20 that had a dependency on EntityFrameworkCore 5.0.0-rc.2.20475.6. Updating the version to the previous one solved the issue: <PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="3.0.20" />