Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Stored procedure #153


User avatar
0
mohammed al-qaisi created

Hello everybody:

i'm trying to execute a stored procedure in my app , i'm using (Module Zero) but i facing issue with (Create Query & Session)

Here is my code

public interface IEmpRepository : IRepository<Employee>
    {
        List<Employee> GetAllWithDepartment(int? deptID);
    }

    public class EmpRepository : EmployeesTasksRepositoryBase<Employee, int>, IEmpRepository
    {
        public EmpRepository(IDbContextProvider<EmployeesTasksDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        public List<Employee> GetAllWithDepartment(int? deptID)
        {
            var query = Abp.Runtime.Session
                .CreateQuery("spGetEmployeesWithUsersWithDepartments")
                .SetParameter("deptId", deptID);               
        }
    }

Thank you all;


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    First of all, what is Abp.Runtime.Session and why are you using it? You should use Context property to get your DbContext. Then if it does not works, please search web for "how to call stored procedure entity framework code first".

  • User Avatar
    0
    mohammed al-qaisi created

    Thank you

    I use this:

    public IEnumerable<Employee> GetAllWithDepartment(int? deptID)
            {
    
                var departmentIDParameter = deptID != null ?
                new SqlParameter("@dept_id", deptID) :
                new SqlParameter("@dept_id", typeof (int));
    
                return Context.Database.SqlQuery<Employee>("spGetEmployeesWithUsersWithDepartments @dept_id", departmentIDParameter);
            }
    
  • User Avatar
    0
    mohammed al-qaisi created

    Hello All;

    My Project structure like this

    • MyProject.Core
    • MyProject.App
    • MyProject.EF [list:20feq927][:20feq927]EmpRepository (Stored procedure here as above implementation)[/:m:20feq927] [:20feq927]MyProject.Test[/:m:20feq927][/list:u:20feq927]

    In my test app

    private readonly IEmpRepository _EmpRepository;
    		//Constructor
    		public UnitTestClass()
            {
               _EmpRepository = LocalIocManager.Resolve<IEmpRepository>();
            }
    		[TestMethod]
            public void Should_Add_Employee_With_Authorized_User()
            {
               var result = _EmpRepository.GetAllWithDepartment(1);
               result.Count().ShouldBe(100);
            }
    
    • I got exception:"The operation cannot be completed because the DbContext has been disposed".
    • Is that the appropriate way to use this (like ref --MyProject.EF -- into --MyProject.Test--).

    Thank you :)

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Have you checked this article: <a class="postlink" href="http://www.codeproject.com/Articles/871786/Unit-testing-in-Csharp-using-xUnit-Entity-Framewor">http://www.codeproject.com/Articles/871 ... y-Framewor</a>

    This article does not cover SPs but can give an idea of testing apps with ABP.

    Are you trying to run tests against an actual database? It's not a suggested way generally since it may be changed and your tests will not be consistent. You can setup and use local DB for unit tests, I did it before, it's slow but covers SPs.