views:

83

answers:

2

I am using .Net Framework 4.0. I have an entity model (.edmx) and I have a menu option in my program for connecting to an existing database or creating a new one. When the user selects to create a new database, I want to generate the database and schema from the entity model. Any idea how to do so?

+1  A: 

Don't do it. It will only work on your v1 products and as soon as you'll plan to release v1.1 you'll realize you have no way of upgrading existing deployed databases to the new schema.

Instead, use scripts to create all your objects (including indexes, relations, constraints, everything) and use upgrade scripts to control how to upgrade each schema version to the next version, like described in Version Control and your Database.

A weaker alternative is to have a Visual Studio Database Project in your solution because VSDB projects have upgrade capabilities, although diff based. They allow you to deploy .dbschema files using the vsdbcmd tool and this tool can diff and upgrade an existing schema. You can have the VSDB project output be transformed into the EF model via an build transformation step (is not trivial, but is possible).

Overall, do not fall for the lure of the EF (or Linq) modeling as being the master definition of your schema. You'll pay the bigger price later.

Remus Rusanu