0

I am using EF Core 6 with Pomelo.EntityFrameworkCore.MySql and code first. I have the following code in my connection string.

public sealed partial class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
        Database.Migrate();

        ChangeTracker.LazyLoadingEnabled = false;
    }

When I run my app and MyDbContext is initialized, for the first time I get Unknown database 'mydatabase' database does not exist but as of now migrations used to create it

User has admin access to mysql so this is not a user permission issue.

This is the stacktrace

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at MySqlConnector.Core.ResultSet.<ReadResultSetHeaderAsync>d__2.MoveNext() in /_/src/MySqlConnector/Core/ResultSet.cs:line 44
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 130
   at MySqlConnector.MySqlDataReader.<CreateAsync>d__106.MoveNext() in /_/src/MySqlConnector/MySqlDataReader.cs:line 457
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at MySqlConnector.Core.CommandExecutor.<ExecuteReaderAsync>d__0.MoveNext() in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 56
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at MySqlConnector.MySqlCommand.<ExecuteNonQueryAsync>d__78.MoveNext() in /_/src/MySqlConnector/MySqlCommand.cs:line 282
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 101
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)

my connection string is "DbConnection": "server=127.0.0.1;port=3306;database=mydatabase;uid=muusername;pwd=mypassword;"

What is the issue here?

pantonis
  • 5,601
  • 12
  • 58
  • 115
  • Does this answer your question? [How and where to call Database.EnsureCreated and Database.Migrate?](https://stackoverflow.com/questions/38238043/how-and-where-to-call-database-ensurecreated-and-database-migrate) – Zer0 Apr 29 '22 at 12:22
  • @Zer0 according to that Migrate() should do the job but it doesn't. Even if database does not exist still it throws this error – pantonis May 20 '22 at 06:54

1 Answers1

0

This is not an exception that is being thrown to your code, but just an internal one that is being caught by Pomelo.

Pomelo is testing, whether the database exists or not, by trying to open it. If it does not exists, the exception is thrown by MySQL and caught by Pomelo, before Pomelo will create the database.

So it is very likely, that you have your IDE setup to break on all (including internal) exceptions. Just try to continue running the app and it should just work fine and create the database.

lauxjpn
  • 4,749
  • 1
  • 20
  • 40