Search code examples
.netentity-frameworkentityentity-framework-ctp5

EF CTP5 Automatically create tables SQL Server 2005


I've been using EF 4 CTP5 and was following scottgu blog post: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

Basically I would like the model to generate the table for me if it is missing.

In my case it is complaining that there is an invalid database object, that's because I don't have a table yet. I'm using SQL Server 2005, whereas the blog mentions sql express or ce.

I would like the mode to create the table, is that feature only for sql express and CE?

Thanks in advance


Solution

  • "Basically I would like the model to generate the table for me if it is missing"

    That scenario is currently not supported using CTP5 however it should be possible to do such a thing in the (near) future: http://blogs.msdn.com/b/efdesign/archive/2010/10/22/code-first-database-evolution-aka-migrations.aspx

    Currently only the following 2 initialization strategies are supported:

    // always recreate the database 
    DbDatabase.SetInitializer(new DropCreateDatabaseAlways<MyDbContext>());
    

    or

    // recreate the database if any changes to the model are detected.
    DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDbContext>());
    

    It should also be noted that EF creates the whole database, not just the tables, so you don't have to create the database yourself.