EF 4.1 Return Results from Stored Procedure

36 views Asked by At

I'm new to EF and have just created a new stored procedure to return some basic info

CREATE PROCEDURE GetTop10Clients
   @AccountId NVARCHAR(50)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT TOP 10  
        Client.Name, Count(Client.Name) AS Number
    FROM           
    WHERE        
        (Client.AccountId = @AccountId)
    GROUP BY 
        Client.Name
    ORDER BY 
        Number DESC
END
GO

I've then added to my edmx and have created a function import called GetTop10Clients to return a complex GetTop10Clients_Result (followed http://www.entityframeworktutorial.net/EntityFramework4.3/execute-stored-procedure-using-dbcontext.aspx)

But I can't figure out how to call it

namespace ClientSystem.Repositories.Ef
{
    public class AccountRepository : IAccountRepository
    {
        public AccountRepository(ObjectSet<Account> set ,ObjectContext ctx) : base(ctx,set)
        {
        }

        public DashboardStats GetTop10Stats(string accountId)
        {
            //var results = GetTop10Clients(accountId); ??????
            return null;
        }
    }
}

Thank you for your help, example would be great.

1

There are 1 answers

0
Jenish Rabadiya On

You will have one function generated in objectcontext/dbcontext like following.

public virtual ObjectResult<GetTop10Clients_Result> GetTop10Clients(Nullable<int> accountId)
{
    .....
}

You can call that method like following.

using (var context = new ObjectContext())
{
    var clients = context.GetTop10Clients(1);
}