2017-11-16 26 views
0

私はaspnetboilerplateテンプレートを使用していますAspnetボイラープレートを使用してEntity Frameworkでストアドプロシージャを呼び出す

私は学生サービスクラスを持っています。私はストアドプロシージャから生徒プロフィールリストを取得しています。

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

インタフェース実装します:

をどのようにインターフェイスを作成
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); 
     } 
    } 

答えて

2

私は
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 } 
      }); 
     } 
    } 

aspnetboilerplateテンプレートにストアドプロシージャを呼び出し、これはサービスクラスであることができます
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

+0

StudentServiceは無限再帰→コンストラクタでIStudentServiceを注入してはいけません!はい、私はそれを削除することを忘れました – Nighil

+0

私はentityframeworkのように使用して呼び出すことができます – Nighil

+0

このような? var courseList = ctx.Database.SqlQuery ( "exec GetCoursesByStudentId @StudentId"、idParam).ToList (); – Nighil

関連する問題