Using database initializers with EDMX
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<T> (or to derive from default ones respectively). Even if you try to wrap ObjectContext into DbContext
you’ll fail. The default objects are derived from EntityObjects 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.
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.