Gary McGill
Gary McGill

Reputation: 27556

How can I initialize a database when using EF MF?

I have an application using EF 4.1 (using the model-first flavour), and I want to initialize the database when the model is updated and the database is dropped and re-created.

I've spent hours trying to find decent documentation on this, and I thought that Database.SetInitializer was the way to go, but it turns out that this requires a DbContext, and the code that's been generated uses ObjectContext.

I believe that I can fiddle about under the hood to switch the T4 templates to use DbContext, but surely there must be a way to work with the out-of-the-box model-first generated code?

[Should I just abandon model-first altogether?]

Upvotes: 1

Views: 114

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

Model first approach doesn't have any automatic database generation.

When you are using model first you simply right click in the designer pane and select Generate database from model. It will create database script which you can execute to create a database. There is additional plugin for VS2010 called Database Generation Power Pack which allows some more advanced scenarios like generating change script between the existing database and your model but this tool requires at least VS2010 Premium.

Upvotes: 3

Related Questions