title: Migrating from Entity Framework 6 Code First MSSQL to MySQL
body: <p>I have worked with EF Code first in MSSQL many times but this is my first time working with it on MySQL.</p>
  
  <p>I have been allocated a MySQL database on AWS with a username and password and using  MySQL Workbench I can see the database and create/drop tables... Happy Days.</p>
  
  <p>I am getting problems with my Code First solution where the package manager when trying to create tables where it doesn't report any errors but the tables are not visible in workbench... They are going somewhere, but I have no idea where...</p>
  
  <p>I have enabled migrations and they are prefixing my table names with</p>
  
  <p>This is my connection string:</p>
  
  <pre><code>&lt;connectionStrings&gt;
  &lt;add name="MyContext" providerName="MySql.Data.MySqlClient"
      connectionString="server=mysql-portal.blahblahblah.us-somewhere-1.rds.amazonaws.com;port=3306;database=mydatabasename;uid=myusername;password=mypassword"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>This is my DbContext:</p>
  
  <pre><code>public class MyContext : DbContext
  {
      public MyContext() : base("MyContext")
      {
          Database.SetInitializer&lt;MyContext&gt;(new MyDbInitializer());
      }
  
      public virtual DbSet&lt;AgentData&gt; AgentDatas { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
          modelBuilder.HasDefaultSchema("mydatabasename");
      }
  }
  public class MyDbInitializer : CreateDatabaseIfNotExists&lt;MyContext&gt;
  {
      protected override void Seed(MyContext context)
      {
          base.Seed(context);
      }
  }
  </code></pre>
  
  <p>When I add a migration the code looks correct:</p>
  
  <pre><code>CreateTable(
              "mydatabasename.AgentData",
              c =&gt; new
                  {
                      Id = c.Long(nullable: false, identity: true),
                      State = c.String(),
                      AgentName = c.String(),
                      AgentLogin = c.String(),
                      AgentPassword = c.String(),
                      NAID = c.String(),
                      License = c.String(),
                      Proxy = c.String(),
                      Enabled = c.Boolean(nullable: false),
                      American_States_id = c.Long(),
                  })
              .PrimaryKey(t =&gt; t.Id)
              .ForeignKey("mydatabasename.American_States", t =&gt; t.American_States_id)
              .Index(t =&gt; t.American_States_id);
  </code></pre>
  
  <p>As I say, when I run this migration and the update the database I don't get any errors.</p>
  
  <p>But I cannot see the table in workbench and if I try to the following it says the table doesn't exist...</p>
  
  <pre><code>public static async Task&lt;List&lt;Model.AgentData&gt;&gt; GetAllAgents()
      {
          using (MyContext db = new MyContext())
          {
              return await db.AgentDatas.ToListAsync();
          }
      }
  </code></pre>
  
  <p>I am stumped... I don't have permissions on any other databases... So where are they going and what am I doing wrong that they don't appear in the correct database as specified by:</p>
  
  <pre><code>modelBuilder.HasDefaultSchema("mydatabasename");
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The strangeness continues.</p>
  
  <p>I can find no reason why the above does not work. So I decided to start a completely clean solution and build it the project from scratch in that solution.</p>
  
  <p>And it works perfectly. I can see my tables and I can add/delete tables to the database.</p>
  
  <p>So, I thought, No problem. I will now add that working project to my other solution. I removed the old solution from my main solution. Deleted the directory. Copied the working project into the main solution directory and added it to the solution...</p>
  
  <p>And it starts doing the same thing... It reports creating the tables in Package Manager with no errors but I cannot see them in Workbench and cannot add data to them.</p>
  
  <p>Both solutions/projects are in Visual Studio Community 2017.</p>
  
  <p>The one that worked was a solution containing just the working project. The solution that doesn't work has multiple existing projects within it.</p>
  
  <p>Why, when I import a working project into the solution does it stop working....</p>
  
  <p>Could anyone hazard a guess?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>This is starting to look more like a bug in VS2017</p>
  
  <p>I have just found all the missing tables and databases in my local MSSQL instance!</p>
  
  <p>It's like when it's in a multi project solution it choosing the local instance of MSSQL and ignoring the connection string to the MySQL database in App.config!</p>
  
  <p>When I go into "Services" and stop my local instance of SQL Server and try to do a Enable-Migrations -Force I get the error</p>
  
  <blockquote>
    <p>The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: The system cannot find the file specified</p>
  </blockquote>