tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Using database initializers with EDMX

21 Nov 2011 Entity Framework

Last week at my Entity Framework training I got a question whether you can use database initializer while still using EDMX file. The answer, as it turned out, isn’t that straightforward.

Directly you can’t. Because you need DbContext to create [IDatabaseInitializer][2] (or to derive from default ones respectively). Even if you try to wrap [ObjectContext][3] into `DbContext` you'll fail. The default objects are derived from [EntityObject][4]s and this is something that's a showstopper for `DbContext`. So that's a bad news.

On the other hand, there’s a good news, kind of. There’s a template (from Microsoft) to generate DbContext and classes from EDMX file.

image

With this template you will get all you need to start using database initializers. Only problem is, that you don’t have configurations generated from EDMX. But there’s also 3rd template, that can generate configurations (I haven’t tested it exhaustively).

Final answer? Yes you can, but it’s not smooth as it could be.