Reputation: 2551
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?
Upvotes: 16
Views: 25265
Reputation: 1419
I had the same issue with:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BreakAwayContext>());
In my case, the database had already existed when i added the above line of code. I dropped the db and ran my program again and it started working as expected. Only other conflict might've been the cause is I had been playing with 'Enable-Migrations' on the database.
HTH
Upvotes: 4
Reputation: 2551
It turned out to be user permissions on the master database. Weird that using DropCreateDatabaseAlways is doesn't need permissions on the master database, where IfModelChanges does.
Upvotes: 8
Reputation: 210080
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.
Upvotes: 11
Reputation: 121902
This behaviour is expected in case you removed IncludeMetadataConvention:
modelBuilder.Conventions.Remove<System.Data.Entity.Database.IncludeMetadataConvention>();
Upvotes: 1