Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add-migration does not function with remote sql server databases in shared hosting

It looks like CodeFirst stops doing its homework when it doesn't have full control of the database (I suppose). The scenario is a web site hosted on Arvixe.com (or I suppose any other shared hosting server), where I have to create databases only from their control panel (and NOT with Sql Server Management Studio, just to say...). Once created an empty database, I register a connection in the web site, and I use it to generate database from poco objects like in:

add-migration m1 -targetdatabase myconnection

This generates correctly my FIRST migration, that I can apply without problems with

update-database -targetdatabase myconnection

The first concern, not too important, is that since the database is existing, it will NOT issue the Seed command, so I have to insert my first records by hand, but this is not a great problem.

Then I change my poco objects, and I need to update the database, but when I issue ANOTHER

add-migration m2 -targetdatabase myconnection

it gives the error:

System.Data.Entity.Migrations.MigrationsPendingException: Unable to generate an explicit migration because the following explicit migrations are pending: [201111081426466_m1]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

This is really strange, since if I look at the database, I can see even the table __MigrationHistory, but then it looks like it cannot recognize it...

Anyone with the same problem, or some good tip to where investigate? Thanks in advance, Andrea Bioli

like image 409
Andrea Avatar asked Nov 14 '22 14:11

Andrea


1 Answers

I had this problem. I was able to resolve it by providing a connectionString and a connectionProviderName parameter to both the Update-Database and the Add-Migration commands.

If you have many projects in your solution with multiple config files, Package Manager seems to be confused. In my case, I had one project selected as the default project for Package Manager Console, but it was pulling the connection string from the Visual Studio solution default start-up project instead.

like image 187
Vladimir Gofaizen Avatar answered Jan 10 '23 15:01

Vladimir Gofaizen