tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Using database initializers with EDMX

21 Nov 2011 1 mins 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<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.

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.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.