首页 > 解决方案 > 在更新 c# 实体框架核心时删除旧文件

问题描述

我正在使用实体框架核心。我有一个实体,它在列内有一个文件的名称。当我更新该实体时,我想从服务器中删除旧文件并上传新文件。

现在我的控制器里面的代码是这样的:

[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)

标签: c#angularentity-framework-core

解决方案


好吧,我已经解决了添加这行代码的问题:

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

而已。真的很简单^^


推荐阅读