【问题标题】:Using PowerShell to create Entity Framework DbContext使用 PowerShell 创建实体框架 DbContext
【发布时间】:2014-05-10 22:21:47
【问题描述】:

我正在尝试为我的基于 EF6 的网站创建 powershell 管理脚本,但我正在努力创建一个 DbContext 来访问我的实体。

PowerShell App.Config 之后,我正在执行以下操作来加载我的 dll,为当前应用程序域设置配置文件并实例化我的 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

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。

有什么想法吗?

【问题讨论】:

    标签: entity-framework powershell


    【解决方案1】:

    事实证明,PowerShell.exe 默认使用 .NET v2.0。按照 cmo999 的answer(请注意,这不是该问题的公认答案),可以通过修改 powershell.exe.config 在 v4.0 下运行 powershell.exe。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 2012-03-03
      • 2019-11-15
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多