Jim_Mcdonalds
Jim_Mcdonalds

Reputation: 496

Pomelo.EntityFrameworkCore.MySql - connection string

I am new to dotnet and EF.

Currently, I am starting a console application and has been getting errors.

DBContext

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
     optionsBuilder.UseMySql(ServerVersion.AutoDetect(
        "server=localhost; port=3306; database=DB; user=root; password=test; Persist Security Info=False; Connect Timeout=300"));
        }

Error

System.InvalidOperationException: A relational store has been configured without specifying either the DbConnection or connection string to use.
   at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.GetConnectionSettings(MySqlOptionsExtension relationalOptions)
   at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.Initialize(IDbContextOptions options)
   at Microsoft.EntityFrameworkCore.Internal.SingletonOptionsInitializer.EnsureInitialized(IServiceProvider serviceProvider, IDbContextOptions options)
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>g__BuildServiceProvider|3()
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>b__2(Int64 k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 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](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
A relational store has been configured without specifying either the DbConnection or connection string to use.

Upvotes: 1

Views: 6940

Answers (1)

SilvanKohler
SilvanKohler

Reputation: 54

I had the same Error Message.

var serverVersion = new MySqlServerVersion(new Version(8, 0, 27));
    
services.AddDbContext<AppDbContext>(options => options.UseMySql("server=localhost;user=user;password=password;database=db", serverVersion));

With this code it works. For more information see in the official documentation from Pomelo:

Upvotes: 4

Related Questions