Sorry to answer such an old question, especially the one marked as the answer, but it haunted me and I wanted to offer a solution independently.
The problem is that LINQPad creates a subclass of the DbContext class. So when you call Database.SetInitializer<MyDbContext>(null) , this will work as long as you create an instance of MyDbContext. But by design, LINQPad compiles your code into a class that comes from MyDbContext. If you make MyDbContext sealed, LINQPad will not be able to use it.
The workaround is to use reflection to invoke Database.SetInitializer in the constructor of the MyDbContext instance. This means that you unnecessarily invoke it for each instance of the context, but that won't hurt anything. But this allows you to use this.GetType () to access the subclass that LINQPad creates, and then you can use reflection to call SetInitializer .
It would be nice if a non-generic version of the method were added.
This Gist has an example of using reflection to invoke Database.SetInitializer .
var databaseType = typeof( Database ); var setInitializer = databaseType.GetMethod( "SetInitializer", BindingFlags.Static | BindingFlags.Public ); var thisType = GetType( ); var setInitializerT = setInitializer.MakeGenericMethod( thisType ); setInitializerT.Invoke( null, new object[] { null } );
source share