Is it possible to generate a db sql script with Ef4 CodeFirst when building?

In the assembly, I want to automatically create a sql script to create a new database. I plan to use this script, so I can create a main database that I can compare with the production database to create a migration script. I can’t use my development database because I have a setting for using SqlCE during development.

Unfortunately, I cannot find anything in the CodeFirst API to create a sql script. I am sure this is possible because the first model does this. I see that the API calls mine DbContextto initialize the database, but nothing that the script gave me to actually initialize it myself.

I also want this script to be generated in the assembly. What is the best way to achieve this? I was thinking of creating a T4 template and using Chirpy to run it in the assembly, but I am wondering if there is a simpler solution.

+5
source share
2 answers

There is no such possibility at the moment. In my version, this may be possible by creating a custom initializer. The initializer will create the database and use SQL Server management objects to create scripts from the database. But it will create scripts based on the current database server, so in the case of SqlCE you will get scripts for SqlCE (there are differences in the types of SQL used).

- SQL Server Express . create script , Visual Studio 2010 diff. .

+2

, ;

- DDL ;

string sqlscript = (context as IObjectContextAdapter).ObjectContext.CreateDatabaseScript();
+2

All Articles