![](/img/trans.png)
[英]How to call a stored procedure in EF Core 3.0 via FromSqlRaw
[英]EF Core 3.0 Stored Procedure : Multiple Parameters
我已經完成了這個問題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。 現在在我的后端 controller 中,我嘗試了一些東西:
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)
我怎么解決這個問題?
嘗試以下修改:
var deptName = new MySqlParameter("@DName", department.DepartmentName);
var deptLoc = new MySqlParameter("@DLoc", department.DepartmentLocation);
var departments = _context
.Departments
.FromSqlRaw("CALL InsertDepartments(@DName,@DLoc)", parameters:new[] { deptName, deptLoc })
.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.