Удалить старый файл при обновлении c# Entity Framework ядра - PullRequest
0 голосов
/ 12 февраля 2020

Я использую ядро ​​платформы сущностей. У меня есть объект, который имеет имя файла внутри столбца. Когда я обновляю эту сущность, я хотел бы удалить старый файл с сервера и загрузить новый.

Сейчас код внутри моего контроллера выглядит так:

[HttpPut("{id}")]
    public async Task<IActionResult> PutMaintenance(int id, Maintenance maintenance)
    {
        if (id != maintenance.id)
        {
            return BadRequest();
        }

        Maintenance oldMaintenance = _context.Maintenance.SingleOrDefault(p => p.id == id);
        var uploads = Path.Combine(Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\")), "Resources", "Maintenances", oldMaintenance.invoicePath);
        if ((System.IO.File.Exists(uploads)))
        {
            System.IO.File.Delete(uploads);
        }

        _context.Entry(maintenance).State = EntityState.Modified;

        try
        {
            _repo.Update(maintenance);
            var save = await _repo.SaveAsync(maintenance);
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!MaintenanceExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return NoContent();
    }

Проблема в том, что этот код возвращает

core.js:6014 ERROR Error Code: 500
Message: Http failure response for https://localhost:44308/api/maintenances/5: 500 OK

в моем приложении Angular. Если я удаляю строки для удаления старого файла, все работает нормально.

Это ошибка API:

System.InvalidOperationException: The instance of entity type 'Maintenance' 

cannot be tracked because another instance with the same key value for {'id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.ThrowIdentityConflict(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry.set_State(EntityState value)
   at TranscoopTrips.Controllers.MaintenancesController.PutMaintenance(Int32 id, Maintenance maintenance) in D:\Davide\Progetti\In corso\TranscoopTrips\TranscoopTrips\Controllers\MaintenancesController.cs:line 66
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   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__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   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)

1 Ответ

0 голосов
/ 12 февраля 2020

Ну, я исправил проблему с добавлением этой строки кода:

_context.Entry(oldMaintenance).State = EntityState.Detached; // severs the connection to the Context

Ничего больше. Было действительно просто ^^

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...