Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate db sql script with Ef4 CodeFirst on build?

On build, I want to automatically create a sql script to generate a fresh database. I plan to use this script so I have can create a master database I can compare with the production database, to generate a migration script. I can't use my development DB, because I have it setup to use SqlCE during development.

Unfortunately, I can't seem to find anything in the CodeFirst API to generate a sql script. I'm sure it's possible, because model-first does it. I see the API calls on my DbContext to initialize a database, but nothing that gives me the script to actually initialize it myself.

I also want to have this script generated on build. What is the best way to have that occur? I was thinking to create a T4 template, and use Chirpy to have it run it on build, but I am wondering if there is a simpler solution.

like image 971
KallDrexx Avatar asked Oct 11 '22 11:10

KallDrexx


1 Answers

No there is no such possibility at the moment. In my opition it can be possible by creating custom initializer. The initializer will create the database and use SQL Server Management Objects to create scripts from the database. But it will only create scripts based on current database server so in case of SqlCE you will get scripts for SqlCE (there are differences in used SQL types).

The correct approach in your scenario is installing SQL Server Express edition and use standard features to initialize your database and that instance. Then you will be able either use create script from database or use Visual Studio 2010 database tools to create diff. scripts against your production.

like image 200
Ladislav Mrnka Avatar answered Oct 14 '22 04:10

Ladislav Mrnka