2

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.

Following PowerShell App.Config, I'm doing the following to load my dll, set the config file for the current app domain and instantiate my DbContext.

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

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

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

I've tried catching the TargetInvocationException, but it seems to escape my try-catch somehow.

Any ideas?

Community
  • 1
  • 1
KvanteTore
  • 96
  • 1
  • 6

1 Answers1

1

It turns out that PowerShell.exe uses .NET v2.0 by default. Following cmo999's answer (note that it's not the accepted answer on that question), it's possible to run powershell.exe under v4.0 by modifying powershell.exe.config.

Community
  • 1
  • 1
KvanteTore
  • 96
  • 1
  • 6