mvc 4 ef 5 RRS feed

  • Question

  • Also as for stackoverflow for some reason they don't like me.

    I have started and modeled an EF 5 project using mvc4. I want to re-build my database from scratch. Is there a way to script the database from the current project completely?


    Tuesday, August 28, 2012 3:34 PM


  • Hi,

    If you are using a model, edmx file, then you can open the model in VS and right click to get an option to generate a database from the model.

    If using code first then if you add Database.CreateIfExists() to the constructor of your context then the database should be created for you if it doesn't exist. So changing your connection string to a new database or deleting the current one should force EF to create a new one, obviously this will destroy all data  in the database so a backup would be  good idea. EF will then create the database when it first attempts to use it and so you will have a new empty database.

    Alternatively you could use something like Code First Migrations to generate migrations from your model that would be executed against a database. This is more of a way to manage your schema changes going forward though. If you have migrations setup you can use the -script option to generate a SQL script to execute.

    Information on migrations is here: http://msdn.microsoft.com/en-us/data/jj591621

    Edit: SQL Management Studio has the ability to script a database to a file as well if I remember correctly.

    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Tuesday, August 28, 2012 4:54 PM