c# – 在Entity Framework下了解多个表结果

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

所以我想将所有内容都移到一个Store Procedure中,并以旧的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>();
}

但我遇到的问题是,行.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

错误信息是

More than one result type declared for function ‘WinnersBackendPageInformation’ that does not return IMultipleResults.

我错过了什么?

最佳答案 你没有遗漏任何东西.

实体框架4不支持存储过程中的多个结果集.

如果您阅读博客文章here,您将从EF团队成员那里找到此声明:

Unfortunately we weren’t able to get full support for multiple results into the product this time around. We did, however, add the method Translate<T> to ObjectContext which allows you to materialize objects from a DataReader. So if you have a stored procedure which returns multiple results whose properties directly align with EF objects, then you could get the underlying store connection from the context (context.Connection.StoreConnection), create a command and use it to execute the stored procedure and get back the DataReader. Then you could call Translate<FirstObjectType> and get back an enumerable of those objects, followed by reader.NextResult() and Translate<SecondObjectType>, etc.

所以,你可以使用一些“老派”ADO.NET,或者你也可以试试CodePlex上的EF Extensions项目,它似乎可以为你做管道工作.

点赞