Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF CodeFirst: DropCreateDatabaseIfModelChanges doesn't work

I use the following code in my Global.asax:

DbDatabase.SetInitializer<MyDBContext>
   (new DropCreateDatabaseIfModelChanges<MyDBContext>());

but it doesn't seem to work.

Although my Model has changed and I'm trying to use one of the newly added table's it just says the table could not be found.

Invalid object name 'dbo.TableName'.

If I run this however, it seems to work, and the table is being created:

DbDatabase.SetInitializer<MyDBContext>(new DropCreateDatabaseAlways<MyDBContext>());

It does update my database.

What is it I do wrong?

like image 272
reinder Avatar asked Mar 21 '11 09:03

reinder


Video Answer


1 Answers

For what it's worth, I ran into a lot of trouble with both of the DropCreate strategies because Cassini was still running after I closed the browser. (I had the same problem with IIS Express.) Because the local web server was still running, Application_Start didn't fire again, so the initialization I put there never got executed.

I resolved it by enabling Edit and Continue:

Project properties > Web > Debuggers > Enable Edit and Continue

This forces the local web server to close when the browser closes.

like image 161
Ryan Lundy Avatar answered Oct 08 '22 18:10

Ryan Lundy