使用Entity Framework执行存储过程的存储库模式

Gay*_*yan 3 entity-framework repository-pattern

我正在尝试将存储库模式用于我的vsto项目.

如何使用存储库模式执行存储过程?我正在使用实体框架.代码示例的任何链接都非常有用

sun*_*nil 6

添加到您的通用存储库

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

然后你就可以用任何unitofwork/repository来调用它

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


Gay*_*yan 1

这个链接指导了我。[关联]

但是当你执行存储过程时,你必须输入 SP 名称的“exec”信息 例如:如果 sp 是“sp_aa”

字符串应该是“exec sp_aa”