5

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was afraid of. I guess the best route to go is to create a "Can Create Sql Express Database" unit test, and that way any time I run unit tests, it will recreate the database on the local machine's Sql Express. Thanks :)
2

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.