15

I've written an ASP.Net MVC 3 application using the Code First paradigm whereby when I make a change to the model the Entity Framework automatically attempts to re-create the underlying SQL Server Database via DROP and CREATE statements. The problem is the application is hosted on a 3rd party remote server which limits the number of databases I can have and does not seem to allow me to programmatically execute "CREATE DATABASE..." statements as I gather from this error message:

CREATE DATABASE permission denied in database 'master'.

Is there any way to stop the Entity Framework from dropping and attempting to re-create the whole database and instead make it simply drop the tables and re-create them?

After creating the database manually and running the application I also get the following error I guess as the Entity Framework tries to modify the database:

Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • What does this problem have to do with ASP.NET? – John Saunders May 25 '11 at 23:35
  • @John Saunders: I included ASP.Net in the question as I'm using it to develop the web app together with EF Code First. I agree with your edit in that to be more precise the issue is with EF Code First database initialization. –  May 26 '11 at 00:06

2 Answers2

11

UPDATE: Found this gem through google, it sounds like its exactly what you need: http://nuget.org/Tags/IDatabaseInitializer

You can use a different database initializer. Lets say your context is called SampleContext then your constructor would look like this:

    public SampleContext() 
    {
        System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<SampleContext>()); 
    }

Note that the above is the default initializer. You will probably need to create your own custom initializer by implementing IDatabaseInitializer. Theres some good info here: http://sankarsan.wordpress.com/2010/10/14/entity-framework-ctp-4-0-database-initialization/

Darko
  • 38,310
  • 15
  • 80
  • 107
  • Using the package (Devtalk.EF.CodeFirst.CreateTablesOnly) from the link in your update note has solved the problem! Darko you are awesome! Thank you so much! –  May 25 '11 at 23:55
  • I have since found the following post which is very helpful on the topic of custom database initialization with EF: [link](http://blogs.microsoft.co.il/blogs/gilf/archive/2011/05/30/creating-a-code-first-database-initializer-strategy.aspx) –  Jun 02 '11 at 02:55
  • 1
    Note that this still drops all the tables each time though, so be careful here... – Ralph Lavelle Sep 20 '11 at 11:34
1

Using EF 4.3 with Migrations you do not get this behavior - at least I have not seen it. But I also have this set in my code -

public sealed class DbConfiguration : DbMigrationsConfiguration<DatabaseContext>
{
    public DbConfiguration()
    {
        AutomaticMigrationsEnabled = false;
    }
}
ferventcoder
  • 11,952
  • 3
  • 57
  • 90
  • Thanks @ferventcoder. I don't think I had EF 4.3 Migrations available when I did my project. Sounds like it's a good step forward. –  Mar 08 '12 at 01:48