EF CodeFirst: DropCreateDatabaseIfModelChanges doesn't work

25,141

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

This behaviour is expected in case you removed IncludeMetadataConvention: modelBuilder.Conventions.Remove<System.Data.Entity.Database.IncludeMetadataConvention>();

Share:
25,141
reinder
Author by

reinder

Updated on April 23, 2020

Comments

  • reinder
    reinder about 4 years

    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?

  • reinder
    reinder about 13 years
    in that case it would, but it isn't the case.
  • Julie Lerman
    Julie Lerman about 13 years
    I'm using this in an MVC3 app with cassini. I find that if I rebuild my solution after modifying my model classes and before running again, the drop/create does its job.
  • Chris McGrath
    Chris McGrath over 12 years
    if you are hitting debug or run without debug this is normally the default action in visual studio the only reason i can see this happening is if you modify the code then just re navigate to the site without rebuilding
  • Patrik Lindström
    Patrik Lindström almost 12 years
    But does it not need to look in the master database to check your databasemodel? Where as just always drop it does not require any peek in the Master database. The Master database used to contain the systemobjects but it is in newer version in the resource database (see technet.microsoft.com/en-us/library/ms190940.aspx about resource database and technet.microsoft.com/en-us/library/ms187837.aspx about master database)