返回自定义对象<列表T& gt;从实体框架并分配给对象数据源 [英] Return Custom Object &lt;List T&gt; from Entity framework and assign to Object Data Source
问题描述
我需要一个问题的指导,我正在使用Entity Framework 4.0,具有DAL和BLL,并绑定到页面上的ObjectDataSource.
I need some guidance with an issue, I am using Entity Framework 4.0, I have a DAL and BLL and am binding to ObjectDataSource on the page.
我必须使用 PIVOT
和动态SQL编写存储过程,才能以所需的方式从多个实体返回数据.现在,我试图弄清楚如何获取实体框架以返回可以绑定到页面上的 ObjectDataSource
的自定义对象,自存储以来,我需要使用自定义对象或动态对象该过程可以返回任意数量的列,因此我不能使用强类型的类或实体,并且还需要将其与 ObjectDataSource
绑定.
I had to write a stored procedure using PIVOT
and dynamic SQL to return the data from multiple entities the way I want. Now I am trying to figure out how can I get Entity Framework to return a custom object that I can bind to my ObjectDataSource
on the page, I NEED to use a custom object or a dynamic object since the stored procedure can return any number of columns so I can't use a strongly typed class or entity and I need to be also able to bind this with an ObjectDataSource
.
有人可以指出执行此操作的好方法以及如何定义我的函数吗?请提供一些代码示例.
Can someone point out a good way to do this and how to define my function? With some code examples please.
我读到我应该尝试使用 List< T>
来返回对象,因为EF不支持返回数据表/数据集,到目前为止,我有以下内容,但我知道这是不正确的
I read that I should try to use List<T>
for returning an object since EF does not support returning datatables/datasets, I have the following so far but I know this isn't correct.
我对泛型的使用不多,如果您能指出如何做到这一点,那么我相信这对很多人都会有帮助.请提供该函数的代码示例以及如何将 ObjectDataSource
绑定到返回对象?
I have not worked with generics much, if you could point out how to do this I'm sure this would be helpful for a lot of people. Please provide code examples for the function and how to bind ObjectDataSource
to the return object?
非常感谢您的帮助!
感谢您的帮助理查德,根据您对DbDataRecord的使用建议,这就是我现在的功能
C#函数
public List<DbDataRecord> GetData(int product_id)
{
List<DbDataRecord> availableProducts = new List<DbDataRecord>();
var groupData = context.ExecuteStoreQuery<DbDataRecord>("exec
spGetProducts @ProductID={0}", product_id);
availableProducts = groupData.ToList<DbDataRecord>();
return availableProducts;
}
ObjectDataSource
在ASPX页面中
<asp:ObjectDataSource ID="ODSProductAvailability" runat="server"
TypeName="Project.BLL.ProductBL"
SelectMethod="GetData" >
<SelectParameters>
<asp:SessionParameter Name="product_id" SessionField="ProductID" />
</SelectParameters>
</asp:ObjectDataSource>
现在访问页面时出现此错误:
Right now I'm getting this error when I access the page:
结果类型'System.Data.Common.DbDataRecord'可能不是抽象的,必须包含默认构造函数
这是因为 ExecuteStoreQuery
希望被定义为类或实体吗?我该如何基于存储过程的结果创建一个对象并将其分配给它?
Is this because the ExecuteStoreQuery
expects to be defined class or entity? How can I just create an object based on the stored procedure results and assign it that?
推荐答案
类似这样的内容:
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
string myQuery = @"SELECT p.ProductID, p.Name FROM
AdventureWorksEntities.Products as p";
foreach (DbDataRecord rec in new ObjectQuery<DbDataRecord>(myQuery, context))
{
Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
}
}
这篇关于返回自定义对象<列表T& gt;从实体框架并分配给对象数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!