views:

38

answers:

2

Is there an equivalence in Entity Framework to NHibernate SchemaExport?

Given I have a working Entity-Model, I would like to programmatically initialize a database.

I would like to use this functionality in the setup of my integration tests.

Creating the matching DDL for an Entity-Model would also suffice.

A: 

Nope, and seriously I do wonder why nhibernate bothers having this.

Problem is: an O/R mapper has LESS information about the database than needed for non-trivial setups.

Missing are:

  • Indices, fully configured
  • Information about server side constraints, triggers (yes, there may be some)
  • Information about object distribution over elements like table spaces
  • Information about permissions

I really love a test method (please check that database is good enough for all objects you know), but generation is VERY tricky - been there, done that. You need some serious additional annotations in the ORM to be able to even generate sensible indices.

TomTom
+3  A: 

Yes - given that you're working with Entity Framework 4 (which is, confusingly enough, the second version...)

Edit: This is the way to do it with just EF4. In my original post below is described how to accomplish the same thing with the Code-Only approach in EF CTP3.

How to: Export model to database in EF4
To export a model to database, right-click anywhere in the designer (where you don't have an entity) and choose "Generate database from model..." and follow the steps described in the wizard. Voila!


Original post, targeting EF4 CTP3 and Code-Only: This is code I use in a little setup utility.

var builder = new ContextBuilder<ObjectContext>();

// Register all configurations you need here
builder.Configurations.Add(new EntryConfiguration());
builder.Configurations.Add(new TagConfiguration());

var conn = GetUnOpenedSqlConnection();
var db = builder.Create(conn);

if (db.DatabaseExists())
{ db.DeleteDatabase(); }

 db.CreateDatabase();

It works on my machine (although here I've simplified a little bit for brevity...), so if something does not work it's because I over-simplified.

Note that, as TomTom stated, you will only get the basics. But it's pretty useful even if you have a more complicated schema - you only have to manually write DDL to add the complicated stuff onto the generated DB schema.

Tomas Lycken
It seems that ContextBuilder is actually not part of EF4 but only of the Microsoft ADO.NET Entity Framework Feature Community Technology Preview 3. Is this correct?Also the method above only works with the "Code Only" approach, does it? Or is there a way of registering an existing Model.edmx with the ContextBuilder?
jbandi
@jbandi: That's right... this is the CTP. However, it is even easier to accomplish the same thing in the designer. See my edit.
Tomas Lycken
Thanks!However I was aware of the export functionality via the designer, but this is not what I was looking for, I was looking for a programmatic solution...I will mark your answer as
jbandi