1

Ive been working with a local MySQL database which has been working fine until we deployed to a server. The webapi was able to query and do any CRUD operations while the database was locally. Since it was deployed I got this error: "Can't create database ''; database exists". I can see it over the server explorer, see the data, I used codeFirst to create context and entities. Any ideas?

Thanks!

marsalal1014
  • 1,897
  • 3
  • 17
  • 24
  • I just want to be sure of this, Did you update the source in your connection string? Because maybe you're still connecting to the DB of your local environment. – Esteban Cervantes Dec 14 '16 at 16:20
  • Yes, I updated the connection string with the new source and credentials in the web.config – marsalal1014 Dec 14 '16 at 16:28
  • What's your database initializer configuration? – Andrés Robinet Dec 14 '16 at 17:25
  • Seems I solved it by deleting my db schema in MySql and run the web service and query it. EF created the schema in the db, but this seems not to be the right thing to do. Thoughts? – marsalal1014 Dec 14 '16 at 17:33
  • @AndrésRobinet if you mean if I have this: Database.SetInitializer(new CreateDatabaseIfNotExists()); I dont. I didnt change or added anything from what EF generated after I set up for code First – marsalal1014 Dec 14 '16 at 17:34
  • So if you never changed or added anything, I'm assuming that you never enabled code first migrations, which is something highly recommended before running into production. – Esteban Cervantes Dec 14 '16 at 17:42
  • Have a look at [Database Initialization Strategies in Code-First](http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx) and [Turn off DB Initializer in Code-First](http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx). If you've enabled migrations, I think you can use [MigrateDatabaseToLatestVersion](https://msdn.microsoft.com/en-us/library/hh829293(v=vs.113).aspx) or roll your own initializer. You can also run **DbMigrator** as part of your deploy target. So many choices :D – Andrés Robinet Dec 14 '16 at 18:01
  • @EstebanCervantes, yes I enabled code first migrations – marsalal1014 Dec 14 '16 at 20:58
  • @AndrésRobinet, thanks! I turned the db init off, seems to be the way here. Thanks! – marsalal1014 Dec 14 '16 at 21:13
  • @marsalal1014 I think you should post your solution and mark it as an answer. Though there are some related questions in SO, your problem and the solution you found might be relevant to others. What do you think? – Andrés Robinet Dec 15 '16 at 16:17

1 Answers1

0

As @AndrésRobinet said, I turned the db init off and seemed to do the trick for me. The other options mentioned were valid but not for my case. Thanks!

marsalal1014
  • 1,897
  • 3
  • 17
  • 24