使用PowerShell创建Entity Framework DbContext [英] Using PowerShell to create Entity Framework DbContext

查看:170
本文介绍了使用PowerShell创建Entity Framework DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的基于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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆