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

EntityFramewotk stored procedures #88


User avatar
0
bvz created

What is the best way that you would recoment calling stored procedures through the EntityFramework provider?

Currently I have added the following to the DbContext file:

public virtual int? spr_AddUser(Nullable<int> clientID, string username, string password, string name, string surname, string iDNumber, string email, Nullable<int> createdBy)
        {
            var clientIdPar = new SqlParameter("@ClientID", clientID);

            var usernamePar = new SqlParameter("Username", username);

            var passwordPar = new SqlParameter("Password", password);

            var namePar = new SqlParameter("Name", name);

            var surnamePar = new SqlParameter("Surname", surname);

            var idNumberPar = new SqlParameter("IDNumber", iDNumber);

            var emailPar = new SqlParameter("Email", email);

            var createdByPar = new SqlParameter("CreatedBy", createdBy);

            var retvalPar = new SqlParameter("@retval", SqlDbType.Int);
            retvalPar.Direction = ParameterDirection.Output;
            
            var parameters = new object[] { clientIdPar, usernamePar, passwordPar, namePar, surnamePar, idNumberPar, emailPar, createdByPar, retvalPar };

            this.Database.ExecuteSqlCommand(
                "exec @retval = spr_AddUser @ClientID,@UserName,@Password,@Name,@Surname,@IDNumber,@Email,@CreatedBy",
                parameters);
            return (int) retvalPar.Value;
        }

This works,but I am not sure if this is the best way to call stored procedures when using ABP.

Could someone who is more experienced with ABP point me in the right direction?


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

    Create a custom repository method (<a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/EntityFramework-Integration#DocCustomRepositoryMethods">http://www.aspnetboilerplate.com/Pages/ ... oryMethods</a>) and call SP there. Then inject the repository and call the method wherever you need. Don't add method to DbContext since you can not inject DbContext in application layer.