【问题标题】:Understanding Multiple Tables result under Entity Framework了解实体框架下的多表结果
【发布时间】:2010-12-22 02:55:41
【问题描述】:

为了显示一个页面,我需要从各种表格中获取大量信息,而目前,加载页面大约需要 20 秒,这太可怕了。

所以我想将所有内容都移到一个存储过程中,并以旧的 DataTable 方式获取所有信息。

我明白了

public WinnerPageInformation FindWinnerPageInformation(int calendarId)
{
    BackendPagesContext ctx = new BackendPagesContext(db.Connection);
    IMultipleResults results = ctx.WinnersBackendPageInformation(calendarId);

    return new WinnerPageInformation()
    {
        Challenges = results.GetResult<Challenges>(),
        Content = results.GetResult<ContentWinners>().FirstOrDefault(),
        Einfo = results.GetResult<ContentEmails>().FirstOrDefault(),
        Fields = results.GetResult<SubscriberFields>(),
        Prizes = results.GetResult<Prizes>(),
        Winners = results.GetResult<Winners>()
    };
}

WinnersBackendPageInformation 看起来像这样

public class BackendPagesContext : DataContext
{
    public BackendPagesContext(System.Data.IDbConnection connection) 
        : base(connection) { }

    [Function(Name = "dbo.sp_GetWinnersBackendPageInformation")]
    [ResultType(typeof(JK_ContentWinners))]
    [ResultType(typeof(JK_Winners))]
    [ResultType(typeof(JK_SubscriberFields))]
    [ResultType(typeof(JK_Prizes))]
    [ResultType(typeof(JK_Challenges))]
    [ResultType(typeof(JK_ContentEmails))]
    public IMultipleResults WinnersBackendPageInformation(
        [Parameter(Name = "calendarId", DbType = "Int")] int calendarId)
    {
        IExecuteResult result =
        this.ExecuteMethodCall(this,
                               ((MethodInfo)(MethodInfo.GetCurrentMethod())),
                               calendarId);
        return (IMultipleResults)(result.ReturnValue);
    }
}

public interface IMultipleResults : IFunctionResult, IDisposable
{
    IEnumerable<TElement> GetResult<TElement>();
}

但我面临的问题是,this.ExecuteMethodCall 行抛出一个错误,指出结果不是 MultipleTable 结果。

我的存储过程看起来像

ALTER PROCEDURE sp_GetWinnersBackendPageInformation
    @calendarId numeric = 0
AS
BEGIN 
    SELECT * FROM ContentWinners WHERE calendar_id = @calendarId;
    SELECT * FROM Winners WHERE calendar_id = @calendarId;
    SELECT * FROM SubscriberFields WHERE calendar_id = @calendarId ORDER BY position;
    SELECT * FROM Prizes WHERE calendar_id = @calendarId ORDER BY prizetype_id, to_day, title;
    SELECT * FROM Challenges WHERE calendar_id = @calendarId;
    SELECT * FROM ContentEmails WHERE calendar_id = @calendarId;
END
GO

错误信息是

为不返回 IMultipleResults 的函数“WinnersBackendPageInformation”声明了多个结果类型。

我错过了什么?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-2 entity-framework-4


    【解决方案1】:

    你没有错过任何东西。

    Entity Framework 4 不支持存储过程中的多个结果集。

    如果您阅读博文 here,您会发现 EF 团队成员的以下声明:

    很遗憾,我们这次未能在产品中获得对多个结果的全面支持。但是,我们确实将 Translate&lt;T&gt; 方法添加到 ObjectContext,它允许您从 DataReader 实现对象。因此,如果您有一个返回多个结果的存储过程,其属性直接与 EF 对象对齐,那么您可以从上下文 (context.Connection.StoreConnection) 获取底层存储连接,创建一个命令并使用它来执行存储过程和取回 DataReader。然后你可以调用Translate&lt;FirstObjectType&gt; 并取回这些对象的可枚举,然后是reader.NextResult()Translate&lt;SecondObjectType&gt;,等等。

    因此,您可以使用一些“老式”ADO.NET,或者您可以尝试CodePlex 上的 EF Extensions 项目,它似乎可以为您完成管道。

    【讨论】:

      猜你喜欢
      • 2019-11-10
      • 2011-05-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多