0

I am trying to execute a stored procedure from MySql database in my ASP.Net core 5 API.

My Database context:

public class SharedDBContext : BaseDbContext
{
    #region Variables

    private readonly BaseUserContext _currentUserContext;

    #endregion Variables
    public SharedDBContext(DbContextOptions<SharedDBContext> options, BaseUserContext userContext) : base(options, userContext)
    {
        this._currentUserContext = userContext;
    }

    public void AddAuditTrail(int userAccountId, string actionName, bool isError, string description, string details)
    {
        List<MySqlParameter> paramList = new List<MySqlParameter>();
        paramList.Add(new MySqlParameter("userAccountId", userAccountId));
        paramList.Add(new MySqlParameter("actionName", actionName));
        paramList.Add(new MySqlParameter("isError", isError));
        paramList.Add(new MySqlParameter("description", description));
        paramList.Add(new MySqlParameter("details", details));
        paramList.Add(new MySqlParameter("createdDate", DateTime.UtcNow));

        this.Database.ExecuteSqlRaw($"AuditTrail_INSERT @PARAMS", paramList);
    }
}

My Services class is:

public class AuditTrailService : IAuditTrailService
{
    private readonly SharedDBContext _dBContext;

    public AuditTrailService(SharedDBContext dBContext)
    {
        _dBContext = dBContext;
    }

    public void AddAuditTrail(int userAccountId, string actionName, bool isError, string description, string details)
    {
        _dBContext.AddAuditTrail(userAccountId, actionName, isError, description, details);
    }
}

I made sure to dependency inject the services in the start up.

Finally, in my API, when I try calling the AddAuditTrail function, I get the following error:

{"Unable to cast object of type 'MySql.Data.MySqlClient.MySqlParameter' to type 'MySqlConnector.MySqlParameter'."}

My Stored Procedure:

CREATE DEFINER=`admin`@`%` PROCEDURE `AuditTrail_INSERT`(
userAccountId int,
actionName VARCHAR(30),
isError TINYINT(1),
description VARCHAR(1024),
details VARCHAR(1024),
createdDate DATETIME(6)
)
BEGIN
    INSERT INTO base_auditTrail(
    UserAccountId, ActionName, IsError, Description, Details, 
    CreatedBy, ModifiedBy, CreatedDate, ModifiedBy, IsDeleted,
    DeletedDate, DeletedBy
    ) 
    VALUES(userAccountId, actionName, isError, description, details, null, null, 
    createdDate, null, 0, null, null);
END

1 Answer 1

1

MySqlParameter exists in 2 namespaces: MySql.Data.MySqlClient and MySqlConnector. You need the latter. Look at your using statements on the top of the file and replace using MySql.Data.MySqlClient; with using MySqlConnector;

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your response. But please help me. Now the error changed to {"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AuditTrail_INSERT @PARAMS' at line 1"}
Sorry, I'm not very experienced with sql syntax and stored procedures, You are better open an additional question.
@NourMawla You are probably missing a CALL statement in your SQL? this.Database.ExecuteSqlRaw($"CALL AuditTrail_INSERT(@userAccountId, @actionName, @isError, @description, @details, @createdDate)", paramList);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.