无法将类型 'MySql.Data.MySqlClient.MySqlParameter' 的对象转换为类型 'MySqlConnector.MySqlParameter'
Unable to cast object of type 'MySql.Data.MySqlClient.MySqlParameter' to type 'MySqlConnector.MySqlParameter'
我正在尝试从我的 ASP.Net 核心 5 API 中的 MySql 数据库执行存储过程 API。
我的数据库上下文:
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);
}
}
我的服务 class 是:
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);
}
}
我确保在启动时依赖注入服务。
最后,在我的 API 中,当我尝试调用 AddAuditTrail 函数时,出现以下错误:
{“无法将类型 'MySql.Data.MySqlClient.MySqlParameter' 的对象转换为类型 'MySqlConnector.MySqlParameter'。”}
我的存储过程:
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
MySqlParameter
存在于 2 个命名空间中:MySql.Data.MySqlClient
和 MySqlConnector
。你需要后者。查看文件顶部的 using 语句,并将 using MySql.Data.MySqlClient;
替换为 using MySqlConnector;
我正在尝试从我的 ASP.Net 核心 5 API 中的 MySql 数据库执行存储过程 API。
我的数据库上下文:
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);
}
}
我的服务 class 是:
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);
}
}
我确保在启动时依赖注入服务。
最后,在我的 API 中,当我尝试调用 AddAuditTrail 函数时,出现以下错误:
{“无法将类型 'MySql.Data.MySqlClient.MySqlParameter' 的对象转换为类型 'MySqlConnector.MySqlParameter'。”}
我的存储过程:
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
MySqlParameter
存在于 2 个命名空间中:MySql.Data.MySqlClient
和 MySqlConnector
。你需要后者。查看文件顶部的 using 语句,并将 using MySql.Data.MySqlClient;
替换为 using MySqlConnector;