ASP.NET Core 3.1:System.InvalidCastException:无法将“System.Int32”类型的对象转换为“System.Int64”类型

问题描述

Picture Error

我的控制器:

[HttpDelete("{roomId}")]
    public async Task<IActionResult> Delete(string roomId)
    {
        var affectedResult = await _manageRoomService.Delete(roomId);
        if (affectedResult == 0)
            return BadRequest();

        return Ok();
    }

我的服务:我尝试调试,但它在代码foreach (var image in images) 中停止。

public async Task<int> Delete(string roomId)
{
        var room = await _context.Rooms.FindAsync(roomId);

        if (room == null) 
             throw new EWebHotelException($"Cannot find a room: {roomId}");

        var images = _context.RoomImages.Where(i => i.RoomId == roomId);

        foreach (var image in images)
        {
            await _storageService.DeleteFileAsync(image.ImagePath);
        }
        
        _context.Rooms.Remove(room);

        return await _context.SaveChangesAsync();
}

我在 FileStorageService.cs 中的方法

public async Task DeleteFileAsync(string fileName)
{
        var filePath = Path.Combine(_userContentFolder,fileName);

        if (File.Exists(filePath))
        {
            await Task.Run(() => File.Delete(filePath));
        }
}     

在 RoomImages 表中,列 Id 的类型为 int 32。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)