我正在为我的基于EF6的网站创建强大的管理脚本,但我正在努力创建一个DbContext来访问我的实体。
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.
以下 PowerShell App.Config ,我正在执行以下操作来加载我的dll,设置当前应用程序的配置文件域和实例化我的DbContext。
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我可以加载dll,并可以检查它找到MyProject.MyDbContext类,并且似乎也可以创建对象,但是在执行最后一个语句时,我得到一个TargetInvocationException
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我尝试捕获TargetInvocationException,但似乎以某种方式逃避了我的try-catch。
I've tried catching the TargetInvocationException, but it seems to escape my try-catch somehow.
任何想法?
推荐答案事实证明PowerShell.exe默认使用.NET v2.0。遵循cmo999的回答(注意,该问题不是公认的答案),可以在v4下运行powershell.exe。 0修改powershell.exe.config。
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.
更多推荐
使用PowerShell创建Entity Framework DbContext
发布评论