Powershell 中的实体框架对象集

发布于 2024-10-14 12:53:49 字数 1641 浏览 6 评论 0 原文

我在 Visual Studio 中创建了一个 DLL,它保存我的实体框架 ObjectContext。我正在尝试从 PowerShell 访问其各种 ObjectSet。我这样做是因为我有一些从 Web 服务中提取的 XML 文件,并且我想使用 PowerShell 的 xml 功能(自动属性生成、非致命 $null 评估)将传入的 xml 值映射到实体必须使用 C# Xml 类。基本上我的 PowerShell 脚本是一个数据加载器。

我能够很好地创建和实例化 ObjectContext。我可以使用 $myObjectContext | 查看所有属性获取成员-MemberType 属性。但是,我无法理解项目何时从查询返回到对象集。

我知道在 Linq-to-Entities 中,存在延迟加载,并且仅在枚举集合时才加载对象。我尝试过显式调用扩展方法,但 PowerShell 似乎不提供 lambda 表达式支持。

这是我的问题。我如何知道我的查询何时将被显式枚举?例如,以下是其中一个属性(定义为 ObjectSet> VehicleTypes { get; })。

PS> $myObjectContext.VehicleTypes

产生以下错误,我将其标记为 (TheError) 以供将来参考:

format-default :调用目标已引发异常。 + CategoryInfo :未指定:(:) [格式默认],TargetInitationException +FullyQualifiedErrorId:System.Reflection.TargetInitationException

但是,PS> $myObjectContext.VehicleTypes | Select-Object VehicleTypeID

生成正确的输出(VehicleTypeID 表)

但是,PS> $myObjectContext.VehicleTypes | Select-Object * 给出如上所述的 TheError。

PS> $myObjectContext.VehicleTypes | Sort-Object 似乎总是枚举集合,这是可以理解的,因为它需要查看所有元素来比较它们。

我应该注意的是,在枚举一次集合后,调用 PS>; $myObjectContext.VehicleTypes 不会给出上面的 TheError - 它显示的集合与您所期望的完全一样。这真的很奇怪,但我认为这与延迟加载有关(这就是我上面提到的原因)。

其他人可以向我确认/解释这种行为吗?也许可以给我一些有关在 PowerShell 中使用实体框架的最佳实践的指导?

另外,如果我做类似 PS> 的事情; $myObjectContext.VehicleTypes | Where-Object {$_.VehicleTypeID -eq $vehicleTypeId} 是否足够智能来执行该查询服务器端,或者是否会从数据库中获取所有记录并仅返回我要查询的记录寻找。如果情况是后者,我想我可能会坚持使用 C#(及其不太友好的 xml 语法)来访问数据。

I've created a DLL in Visual Studio that holds my Entity Framework ObjectContext. I'm trying to access its various ObjectSets from PowerShell. I'm doing this because I have some XML files that I'm pulling from a web service and I'd like to use PowerShell's xml features (automatic property generation, non-fatal $null evaluation) to map incoming xml values to Entities instead of having to use the C# Xml classes. Basically my PowerShell script is a data loader.

I am able to create and instantiate the ObjectContext just fine. I can see all properties using $myObjectContext | Get-Member -MemberType Property. However, I'm having trouble understanding when exactly items get returned from queries to the ObjectSet.

I know that in Linq-to-Entities, there is lazy loading and that objects are only loaded when the collection is enumerated. I've tried calling extenion methods explicity, but it looks like PowerShell doesn't provide lambda expression support.

Here's my question. How do I know when my queries are going to be explicitly enumerated? For example, here's one of the properties (defined as ObjectSet<VehicleType> VehicleTypes { get; }).

PS> $myObjectContext.VehicleTypes

Produces the following error, which I'll label as (TheError) for future reference:

format-default : Exception has been thrown by the target of an invocation.
+ CategoryInfo : Not Specified: (:) [format-default], TargetInvocationException
+ FullyQualifiedErrorId : System.Reflection.TargetInvocationException

BUT, PS> $myObjectContext.VehicleTypes | Select-Object VehicleTypeID

produces the correct output (a table of VehicleTypeIDs)

However, PS> $myObjectContext.VehicleTypes | Select-Object * gives TheError as described above.

PS> $myObjectContext.VehicleTypes | Sort-Object always seems to enumerate the collection, which is understandable since it needs to look at all elements to compare them.

I should note that after the collection is enumerated once, calling PS> $myObjectContext.VehicleTypes does NOT give TheError above - it display the collection exactly as you'd except. This is really weird, but I think it has something to do with lazy loading (which is why I mentioned it above).

Can anyone else confirm/explain this behavior to me, and maybe give me some pointers on best practices for using Entity Framework with PowerShell?

Also, if I do something like PS> $myObjectContext.VehicleTypes | Where-Object {$_.VehicleTypeID -eq $vehicleTypeId} is it going to be smart enough to execute that query server side, or is it going to fetch all records from the DB and return just the one I'm looking for. If the situation is the latter, I think I might be stuck with using C# (with its not-so-friendly xml syntax) for my data access.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-10-21 12:53:49

我可以告诉你,这段代码执行的枚举:
PS> $myObjectContext.VehicleTypes |地点对象 {$_.VehicleTypeID -eq $vehicleTypeId}
都是客户端。

如果要执行本机筛选,则需要创建 cmdlet 来访问数据,或创建支持本机筛选器的提供程序。

I can tell you that the enumeration performed by this code:
PS> $myObjectContext.VehicleTypes | Where-Object {$_.VehicleTypeID -eq $vehicleTypeId}
is all client-side.

If you want to perform native filtering, you'll need to create a cmdlet to access the data, or a provider that supports native filters.

迟到的我 2024-10-21 12:53:49

我意识到有点晚了,但 TheError 可能是由于 powershell.exe 运行 .net v2.0 造成的。请参阅cmo999的答案,以及我自己的类似问题

可以通过添加/修改 powershell.exe.config 将 powershell.exe 配置为在 .net 4.0 下运行,如上面链接中的 cmo999 所述。

A bit late, I realize, but TheError might be due to powershell.exe running .net v2.0. See cmo999's answer, and my own similar question.

It's possible configure powershell.exe to run under .net 4.0 by adding/modifying powershell.exe.config as described by cmo999 in the link above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文