Home > OS >  MySqlException: Table 'xxxxx' doesn't exist - Ef6
MySqlException: Table 'xxxxx' doesn't exist - Ef6

Time:09-21

I am using Entity Framework 6 in a WPF project and the initial migrations and database creation is all right and working. My problem is that I have been working on a local database on my machine, however when I try to connect to a remote database I get this error. DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. MySqlException: Table 'db.tester' doesn't exist

This exception was originally thrown at this call stack:
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in ExceptionDispatchInfo.cs
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) in TaskAwaiter.cs
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) in TaskAwaiter.cs
MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(MySqlConnector.Protocol.Serialization.IOBehavior) in ResultSet.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in ExceptionDispatchInfo.cs
MySqlConnector.MySqlDataReader.ActivateResultSet(System.Threading.CancellationToken) in MySqlDataReader.cs
MySqlConnector.MySqlDataReader.CreateAsync(MySqlConnector.Core.CommandListPosition, MySqlConnector.Core.ICommandPayloadCreator, System.Collections.Generic.IDictionary<string, MySqlConnector.Core.CachedProcedure>, MySqlConnector.Core.IMySqlCommand, System.Data.CommandBehavior, System.Diagnostics.Activity, MySqlConnector.Protocol.Serialization.IOBehavior, System.Threading.CancellationToken) in MySqlDataReader.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in ExceptionDispatchInfo.cs
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) in TaskAwaiter.cs
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) in TaskAwaiter.cs

The remote database is working as I can access it on MySql workbench. I have also tried to include Database.EnsureCreated() within the dbContext constructor but it is still not finding the database table. What am I missing?

CodePudding user response:

I discovered that database.EnsureCreated() was not the right function since my issue was that the database in the first place had not been migrated. Using database.Migrate() solved my problem. Since I put this in the constructor of the DBContext class, it runs every time the app starts, however, the EF migrations only happens once. The problem however is that it throws an error of - this table already exists. To avoid this I used a try and catch

public SettingsDbContext()
    {
        try
        {
            this.Database.Migrate();
        }catch(Exception exp)
        {
           // MessageBox.Show(exp.ToString());
        }      
    }
  • Related