Reputation: 27803
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.
Upvotes: 5
Views: 905
Reputation: 5240
For any one that's still looking;
one option is you could get generated DDL as a string and save it to to a file;
string sqlscript = (context as IObjectContextAdapter).ObjectContext.CreateDatabaseScript();
Upvotes: 2
Reputation: 364279
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.
Upvotes: 2