使用 Aspnet 样板调用 Entity Framework 中的存储过程

Calling Stored Procedure in Entity Framework using Aspnet boilerplate

我正在使用 aspnetboilerplate 模板

我有一个学生服务 class。我正在从存储过程中获取学生资料列表。我如何在 aspnetboilerplate 模板中调用存储过程

  public class StudentRepository : TabonoRepositoryBase<User, long>
    {
        private readonly IActiveTransactionProvider _transactionProvider;

        public StudentRepository(IDbContextProvider<TabonoDbContext> dbContextProvider, IActiveTransactionProvider transactionProvider)
            : base(dbContextProvider)
        {
            _transactionProvider = transactionProvider;
        }

        //TODO: Make async!
        public async Task<int> GetProfileCompletePercentage(int studentid)
        {
            EnsureConnectionOpen();

            using (var command = CreateCommand("Sp_GetStudentprofilepercentage", CommandType.StoredProcedure, new SqlParameter("StudentId", studentid)))
            {
                using (var dataReader = await command.ExecuteReaderAsync())
                {
                    while (dataReader.Read())
                    {
                        return Convert.ToInt16(dataReader["TotalPer"].ToString());
                    }
                    return 0;
                }
            }
        }

        private DbCommand CreateCommand(string commandText, CommandType commandType, params SqlParameter[] parameters)
        {
            var command = Context.Database.GetDbConnection().CreateCommand();

            command.CommandText = commandText;
            command.CommandType = commandType;
            command.Transaction = GetActiveTransaction();

            foreach (var parameter in parameters)
            {
                command.Parameters.Add(parameter);
            }

            return command;
        }

        private void EnsureConnectionOpen()
        {
            var connection = Context.Database.GetDbConnection();

            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
        }

        private DbTransaction GetActiveTransaction()
        {
            return (DbTransaction)_transactionProvider.GetActiveTransaction(new ActiveTransactionProviderArgs
            {
                {"ContextType", typeof(TabonoDbContext) },
                {"MultiTenancySide", MultiTenancySide }
            });
        }
    }

这是服务class

 public class StudentService : AsyncCrudAppService<StudentCore, StudentDto, int, PagedResultRequestDto, StudentCreateDto, StudentUpdateDto>, IStudentService
    {
        public readonly IRepository<StudentCore> _studentRepository;
        private readonly UserManager _userManager;
        private readonly IStudentService _studentservice;

        public StudentService(IRepository<StudentCore> repository, UserManager um, IStudentService studentservice) : base(repository)
        {
            _studentRepository = repository;
            _userManager = um;
            _studentservice = studentservice;
        }
 public Task GetProfileCompletePercentage(int studentid)
        {
          return  _studentservice.GetProfileCompletePercentage(studentid);
        }
    }

创建接口:

public interface IStudentRepository : IRepository<StudentCore>
{
    Task<int> GetProfileCompletePercentage(int studentid);
}

实现接口:

public class StudentRepository : TabonoRepositoryBase<StudentCore>, IStudentRepository
{
    // ...
}

注入接口并调用方法:

public class StudentService : ...
{
    private readonly IStudentRepository _studentRepository;

    public StudentService(IStudentRepository repository) : base(repository)
    {
        _studentRepository = repository;
    }

    public Task GetProfileCompletePercentage(int studentid)
    {
        return _studentRepository.GetProfileCompletePercentage(studentid);
    }
}

注意:StudentService不能在构造函数中注入IStudentService→无限递归!

供参考:https://www.codeproject.com/Articles/1199648/Using-Stored-Procedure-User-Defined-Function-and-V