【发布时间】:2019-11-20 10:00:22
【问题描述】:
我已经解决了这个问题How to call a stored procedure in EF Core 3.0 via FromSqlRaw。但这并不能解决我的问题。我正在使用 MySql 数据库。在我的数据库中有一个存储过程,我在其中插入一些数据,然后返回整个表。
我的存储过程代码是这样的:
DELIMITER $$
Create Procedure InsertDepartments(DName longtext,DLoc longtext)
BEGIN
INSERT INTO Departments (DepartmentName, DepartmentLocation)
VALUES (Dname, DLoc);
SELECT * FROM Departments ORDER By Departments.DepartmentId DESC;
END$$
如您所见,我正在尝试使用两个参数 DName 和 DLoc。现在在我的后端控制器中,我尝试了一些东西:
public JsonResult InsertDepartment([FromBody]Department department)
{
try
{
var deptName = new MySqlParameter("Dname", department.DepartmentName);
var deptLoc = new MySqlParameter("DLoc", department.DepartmentLocation);
var departments = _context
.Departments
.FromSqlRaw("EXECUTE InsertDepartments @Dname , @DLoc", new object[] {deptName, deptLoc})
.ToList();
return Json(departments);
}
catch (Exception e)
{
return Json(e.Message);
}
}
但这在某种程度上失败了。 PostMan 中返回的错误日志是:
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''CSE' , 'DT5'' at line 1"
错误日志:
System.NotSupportedException: Specified method is not supported.
at System.Text.Json.JsonSerializer.WriteDictionary[TProperty](JsonConverter`1 converter, JsonSerializerOptions options, WriteStackFrame& current, Utf8JsonWriter writer)
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnWriteDictionary(WriteStackFrame& current, Utf8JsonWriter writer)
at System.Text.Json.JsonPropertyInfo.WriteDictionary(WriteStack& state, Utf8JsonWriter writer)
at System.Text.Json.JsonSerializer.HandleDictionary(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteObject(JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultFilters>g__Awaited|27_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
我该如何解决这个问题?
【问题讨论】:
-
对 MySql/MariaDB 不是很熟悉,但你确定你使用的是正确的语法吗?我以为他们用
CALL来执行存储过程? -
还是一样的@StephenByrne
-
向我们展示您正在执行的 sql 文本。也许你可以尝试复制粘贴到管理工作室看看错误
-
@LorenzoIsidori 我不知道您在说哪个 SQL 查询。但是我添加了详细信息错误日志。你能检查一下吗?
-
错误来自
return Json(departments)或return Json(e.Message)???
标签: c# asp.net-core stored-procedures .net-core entity-framework-core