3

Database.SetInitializer() works great for testing, but I need the SQL output in a file for working in a production environment (DBAs aren't going to run a program to create the DB). Castle ActiveRecord make output to a file easy with ActiveRecordStarter.GenerateCreationScripts(). If there's a similar method in EF, I'm unable to find any mention of it. Is this doable?

3 Answers 3

9

You can also use the DbContext to get the Sql DDL.

string sqlscript = (context as IObjectContextAdapter).ObjectContext.CreateDatabaseScript();
Sign up to request clarification or add additional context in comments.

4 Comments

sweet! didnt know about this, the string can be saved into a file;
I think I liked this answer best :-)
Yes, definitely the best answer.
Just tested this and it generates the schema properly, but does not include nonclustered indexes, like the ones included when you have code-first recreate the db from within your app. Otherwise the schemas are the same.
3

Well you could run the Database.SetInitializer() method to create the initial database for you and then once you are satisfied with the database schema then you could possibly use something like SQL Server Management Studio to generate the script from the database

2 Comments

+1 I think it is the only option in the current version of the API.
SQL Server Management Studio did what I needed. Thanks much.
0

i think the Entity Designer Database Generation Power Pack is probably what you're looking for.

1 Comment

It doesn't work with the code first. It works only with the model first using EDMX file.

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.