11

What am I doing wrong. I have got a user DbContext setup and working when I originally created the Code-First with powershell it all worked fine.

I implemented Database Initializer as expected on application start.

Database.SetInitializer<UserDbContext>(new CreateDatabaseIfNotExists<UserDbContext>());

Just to test out if it really creates the database I actually dropped the database and now I am stuck the database will not be created. I am using SQL Server 2012, any idea what could be wrong.

The error message I am getting is

System.InvalidOperationException: Migrations is enabled for context 'UserDbContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.

I have tried the same from Package Manager console and it is still give me the same message.

Sanj
  • 3,770
  • 6
  • 26
  • 31

2 Answers2

14

Finally figured the solutions, not sure why or what. Changed my Database initializer to MigrateDatabaseToLatestVersion instead of CreateDatabaseIfNotExists worked.

Database.SetInitializer<UserDbContext>(new MigrateDatabaseToLatestVersion<UserDbContext, Configuration>());
Sanj
  • 3,770
  • 6
  • 26
  • 31
2

Edit: With your new error message the problem comes from you having migrations enabled and already ran a migration (probably the first creation of the database) and since you dropped the DB the migration history has been lost. If your not using Automatic migrations you can not go in and make changes to the database your self and expect code-first to know about it. Try disabling migrations and re-enabling them to see if that clears out the migration history.

You will need to make a call into the DB either as a read or insert of data for the DB to initially be created. The code you use only tells EF how to deal with a database if one does not exist when it tries to find it. For me I use the method outlined at http://www.codeproject.com/Tips/411288/Ensure-Your-Code-First-DB-is-Always-Initialized

Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
  • Thanks for your response. I have tried that still doesn't work. – Sanj Oct 23 '13 at 15:50
  • Odd it worked for me, check out this SO about different methods of initializing your DB for more ideas: http://stackoverflow.com/questions/12724320/forcing-code-first-to-always-initialize-a-non-existent-database – Matthew Verstraete Oct 23 '13 at 15:53
  • Hi Eagle, I have followed you SO and have pretty much got everything accordingly. What you mentioned I am using Auto-migrations and Migrations history is kept inside the database itself right? Or am I missing a big piece of the picture here. Theoritically delete a database with Auto-Migration enabled with DB initialization rule should recreate the database. Still no luck.... – Sanj Oct 23 '13 at 16:10
  • Migration date is stored in special table in the database so when auto-migrations runs it looks for the DB spec from that special table and compares it to the new dbcontext schema and applies any changes. Since the table is missing it has nothing to compare to and fails as it is expecting the database to exist. What you need to do is manualy run migration 0 (Read Generating & Running Migrations section @ http://msdn.microsoft.com/en-us/data/jj591621.aspx) or disable then reenable migrations – Matthew Verstraete Oct 23 '13 at 16:16