[英]ASP.NET Core EF - Getting an error when uploading a file
I am developing a system using ASP.NET Core, EF Core and my database are MySQL and I am using MySQL Connector.NET as the adapter.我正在开发一个使用 ASP.NET Core、EF Core 的系统,我的数据库是 MySQL,我使用 MySQL Connector.NET 作为适配器。
I have a place in my system where the user can choose a file and upload.我的系统中有一个地方供用户选择文件并上传。 My question is, when I try to upload a file which is more than 1000 KB, I am getting the following error:
我的问题是,当我尝试上传超过 1000 KB 的文件时,出现以下错误:
An established connection was aborted by the software in your host machine
已建立的连接被主机中的软件中止
The database field for this file is of a LONGBLOB
type and the type of the modal is byte[]
.该文件的数据库字段是
LONGBLOB
类型,模态类型是byte[]
。 The error is thrown right inside the dbcontext.SaveChanges()
method which is why it is hard to debug and identify the reason.该错误直接在
dbcontext.SaveChanges()
方法中抛出,这就是为什么很难调试和确定原因的原因。
I will post the full stack trace below:我将在下面发布完整的堆栈跟踪:
at MySqlConnector.Protocol.Serialization.SocketByteHandler.WriteBytesAsync(ArraySegment`1 data, IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\Protocol\Serialization\SocketByteHandler.cs:line 90
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySqlConnector.Utilities.ValueTaskExtensions.<ContinueWith>d__0`2.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Utilities\ValueTaskExtensions.cs:line 8
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySqlConnector.Core.ServerSession.TryAsyncContinuation(Task`1 task) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 1225
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__1.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 36
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySql.Data.MySqlClient.MySqlCommand.<ExecuteNonQueryAsync>d__60.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 62
at MySql.Data.MySqlClient.MySqlTransaction.Dispose(Boolean disposing) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlTransaction.cs:line 81
at Microsoft.EntityFrameworkCore.Storage.RelationalTransaction.Dispose()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple`2 parameters)
at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Zoneberry.Repository.ZONContext.SaveChanges() in path\ZONContext.cs:line 157
at path.Commit() in project\UnitOfWork.cs:line 419
at project.Controller.UploadFileFor(List`1 files, Int32 leadId, Int32 totalCount) in project\Controllers\LeadController.cs:line 220
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
The system works fine when I upload files which are smaller than 1MB.当我上传小于 1MB 的文件时,系统工作正常。
Probably you have a limit in a MySql
side.可能您在
MySql
方面有限制。 Try to execute this command on MySql
server:尝试在
MySql
服务器上执行此命令:
SHOW VARIABLES LIKE 'max_allowed_packet';
If the result is near 1 million
bytes, then you need to increase that value (eg almost 2 MB) by running the following command:如果结果接近
1 million
字节,那么您需要通过运行以下命令来增加该值(例如几乎 2 MB):
SET GLOBAL max_allowed_packet=2000000;
But this setting will be reset after server reboot.但此设置将在服务器重新启动后重置。 To change the setting permanently you need to change it in
my.ini
or ~/.my.cnf
under [mysqld]
section:要永久更改设置,您需要在
[mysqld]
部分下的my.ini
或~/.my.cnf
更改它:
max_allowed_packet=2M
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.