Repository pattern to execute a stored procedure using Entity Framework

entity-framework repository-pattern

Question

I'm trying to use repository pattern for my vsto project.

How do I use a repository pattern to execute a stored procedure? I'm using Entity Framework. Any link for code sample would really useful

1
3
8/13/2013 6:12:41 AM

Accepted Answer

this link guided me. [Link]

But when you execute stored procedure you have to put "exec" informant of SP name Eg: if sp is "sp_aa"

string should be "exec sp_aa"

1
8/15/2013 11:36:30 AM

Popular Answer

To your generic repository add

public IEnumerable<T> ExecWithStoreProcedure(string query, params object[] parameters)
{
        return _context.Database.SqlQuery<T>(query, parameters);
}

And then you can call it with any unitofwork/repository like

IEnumerable<Products> products = 
             _unitOfWork.ProductRepository.ExecWithStoreProcedure(
             "spGetProducts @bigCategoryId",
             new SqlParameter("bigCategoryId", SqlDbType.BigInt) { Value = categoryId } 
      );


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow