Search code examples
.netentity-framework-4mdfsql-scriptsef-model-first

How to run an SQL script against a MDF file?


I've created a database model with model-first method using Entity Framework 4.0. I then created an sql script using the Generate Database from Model... I've also created an SQL Server Database file in my App_Data folder. How do I now run the SQL file against this MDF file?

I'm using Visual Studio 2010.


Solution

  • I found a solution, but it's a bit hacky.

    I have SQL Server Express (2008 R2). So when generating the database from the model I connect to it and let it build a database there. Then I go to C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA. The 10_50 part is because of the version and can be different for you. So in this folder there's a .mdf file named just like the database - .mdf. There's also a _log.ldf file. I copied these to into the App_Data folder of my project.

    This works but is very time consuming to generate a new .mdf database for every change in the model. So I do this just before shipping.

    If you find a better answer please do share.