Pomelo.entityframeworkcore.mysql: EfCore / Pomelo 5.0 : Error "A relational store has been configured without specifying either the DbConnection or connection string to use."

Created on 16 Nov 2020  路  1Comment  路  Source: PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

Steps to reproduce

Configure a DbContext with EntityFrameworkCore 5.0 / Pomelo 5.0.
Error "A relational store has been configured without specifying either the DbConnection or connection string to use." occurs during the call to Migrate method.

The issue

DbContext can't be instancied during the call to "Migrate" with the exception bellow :

16/11/2020 08:49:18 STDOUT | System.InvalidOperationException: Upgrade error
16/11/2020 08:49:18 STDOUT | ---> System.InvalidOperationException: A relational store has been configured without specifying either the DbConnection or connection string to use.
16/11/2020 08:49:18 STDOUT | at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.GetConnectionSettings(MySqlOptionsExtension relationalOptions)
16/11/2020 08:49:18 STDOUT | at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.Initialize(IDbContextOptions options)
16/11/2020 08:49:18 STDOUT | at Microsoft.EntityFrameworkCore.Internal.SingletonOptionsInitializer.EnsureInitialized(IServiceProvider serviceProvider, IDbContextOptions options)
16/11/2020 08:49:18 STDOUT | at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.g__BuildServiceProvider|3()
16/11/2020 08:49:18 STDOUT | at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.b__2(Int64 k)
16/11/2020 08:49:18 STDOUT | at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
16/11/2020 08:49:18 STDOUT | at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
16/11/2020 08:49:18 STDOUT | at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
16/11/2020 08:49:18 STDOUT | at DataBase.TruckFleetContext..ctor(DbContextOptions1 dbContextoption) in /home/vsts/_work/1/s/DataBase/TruckFleetContext.cs:line 14 16/11/2020 08:49:18 STDOUT | at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) 16/11/2020 08:49:18 STDOUT | at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) 16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.b__0(ServiceProviderEngineScope scope)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
16/11/2020 08:49:18 STDOUT | at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredServiceT

DbContext are declared in the ConfigureServices with :

            var test = Configuration.GetConnectionString("ConnStringName");
            services.AddDbContext<DbContext>(a => {
                Console.WriteLine($"Current ConnectionString: {Configuration.GetConnectionString("ConnStringName")}");
                a.UseMySql(ServerVersion.AutoDetect(Configuration.GetConnectionString("ConnStringName")));
                if (AwsDetector.IsAwsLambaEnv())
                {
                    a.AddXRayInterceptor(true);
                }
            });

Note that the Console output with the connection string output a valid connection string like :

Data Source=rdsname.eu-central-1.rds.amazonaws.com;Initial Catalog=dbname;user ID=username;password=password;Pooling=false;

Tryed to remove Aws Xray, but does not fix the problem.

Further technical details

MySQL version: 5.6.10 - MySQL Community Server (GPL) (AWS Aurora RDS instance)
Operating system: (Lambda - Amazon Linux 2)
Pomelo.EntityFrameworkCore.MySql version: 5.0.0-alpha.2
Microsoft.AspNetCore.App version: N.A (using shared framework throught framework reference)

Other details about my project setup:

Running on Aws Lambda.

closed-question type-question

Most helpful comment

Problem found : on the UseMysql, connection string must be pass two time (one in the connectionstring parameters, and one to the ServerVersion Call)

Invalid :

a.UseMySql(ServerVersion.AutoDetect(Configuration.GetConnectionString("ConnStringName")));
Valid :

a.UseMySql(Configuration.GetConnectionString("ConnStringName"), ServerVersion.AutoDetect(Configuration.GetConnectionString("ConnStringName")));

>All comments

Problem found : on the UseMysql, connection string must be pass two time (one in the connectionstring parameters, and one to the ServerVersion Call)

Invalid :

a.UseMySql(ServerVersion.AutoDetect(Configuration.GetConnectionString("ConnStringName")));
Valid :

a.UseMySql(Configuration.GetConnectionString("ConnStringName"), ServerVersion.AutoDetect(Configuration.GetConnectionString("ConnStringName")));

Was this page helpful?
0 / 5 - 0 ratings