How can I get a DataSet as the result of a query in Entity Framework 5.0?(作为 Entity Framework 5.0 中的查询结果,如何获取 DataSet?)
问题描述
我不确定这是否可能.我可能正在尝试使用错误的工具来完成我正在尝试做的工作.
I'm not sure if this is even possible. I might be trying to use the wrong tool for the job I'm trying to do.
假设我想做以下事情:
我执行了一个名为GetTableName"的函数,它返回我要从中获取数据的 SQL 表的名称.
I execute a function called "GetTableName" that returns the name of a SQL table I want to get data from.
我执行另一个名为GetFields"的函数,它返回 SQL 表中一个或多个字段的名称.
I execute another function called "GetFields" that returns a the names of one or more fields in the SQL table.
我希望能够编写另一个函数,该函数将返回一个 DataSet(或其他一些通用对象),其中包含GetTableName"指定的表中的行以及GetFields"指定的字段
I would like to be able to write another function that would return a DataSet (or some other generic object) containing rows from the table specified by "GetTableName" with the fields specified by "GetFields"
我知道我可以使用旧的 ADO.net 界面来做到这一点,但是有没有合适的方法来使用实体框架来做到这一点?
I know that I can do this with the old ADO.net interface, but is there an appropriate way to do this with Entity Framework?
强调一下,代码在编译时将不知道从 SQL 返回的数据的形状.
To emphasize, the code will not know at compile time what the shape of the data returned from SQL will be.
推荐答案
由于 EF 需要映射到复杂类型,因此可能编写一个继承自 ExpandoObject 的类,然后您就可以(通过大量编码)进行映射到 DataTable 类上或按原样使用.然后你基本上需要告诉 EF 以某种方式尝试映射到这些属性.但是,我认为这将是一段漫长而曲折的代码、重载和基本重写 EF:s 对象映射的核心功能的旅程.
Since EF requires complex types to map onto, it's probably possible to write a class inheriting from ExpandoObject which you would then be able (with alot of coding) to map onto a DataTable class or just use as is. Then you'd basically need to tell EF to somehow try map to those properties. However, I think that would be a long and winding journey of code, overloads and basicly rewriting the core functionality of EF:s object mapping.
正如您在此示例中所见,作者动态创建模型,但仍然存在困难;根据数据自动生成属性的部分:http://www.markzhou.com/blog/post/2011/06/02/Use-dynamic-type-in-Entity-Framework-41-SqlQuery()-method.aspx
As you can see in this example, where the writer dynamicly creates the model, the hard still remains; the part where you automaticly generate the properties based on the data: http://www.markzhou.com/blog/post/2011/06/02/Use-dynamic-type-in-Entity-Framework-41-SqlQuery()-method.aspx
如果您需要 DataTable,我真的建议您使用 ADO.NET.
I'd really recommend you go with ADO.NET if you need DataTable.
这篇关于作为 Entity Framework 5.0 中的查询结果,如何获取 DataSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:作为 Entity Framework 5.0 中的查询结果,如何获取 DataSet?
基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
