title: Using PowerShell to create Entity Framework DbContext
body: <p>I'm trying to create powershell management scripts for my EF6 based website, but I'm struggling to create a DbContext to access my entities.</p>
  
  <p>Following <a href="https://stackoverflow.com/questions/17960/powershell-app-config">PowerShell App.Config</a>, I'm doing the following to load my dll, set the config file for the current app domain and instantiate my DbContext.</p>
  
  <pre><code>Add-Type -AssemblyName System.Configuration
  [appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "path.to.web.config")
  
  [Reflection.Assembly]::LoadFrom("MyProject.Dll")
  $ctx = New-Object MyProject.MyDbContext
  $ctx
  </code></pre>
  
  <p>I'm able to load the dll, and can inspect that it finds the MyProject.MyDbContext class, and it seems to be able to create the object as well, but upon executing the last statement I get a TargetInvocationException</p>
  
  <pre><code>format-default : Exception has been thrown by the target of an invocation.
      + CategoryInfo          : NotSpecified: (:) [format-default], TargetInvocationException
      + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,Microsoft.PowerShell.Commands.FormatDefaultCommand
  </code></pre>
  
  <p>I've tried catching the TargetInvocationException, but it seems to escape my try-catch somehow.</p>
  
  <p>Any ideas?</p>