使用PowerShell创建Entity Framework DbContext

编程入门 行业动态 更新时间:2024-10-24 06:35:34
本文介绍了使用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

本文发布于:2023-11-14 00:50:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Entity   PowerShell   DbContext   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!